SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod rewrite - redirect problem

    I have created a simple dynamic website with php and I am having trouble making my URL structure search engine friendly.
    What I am trying to do is redirect links like this
    HTML Code:
    www.example.com/index.php?page=animals
    to this (including a trailing backslash)
    HTML Code:
    www.example.com/animals/
    I have spent ages searching for a solution and found the below is the only one that actually lets the new URL work but I can't redirect it.
    It seems to be backwards to every other solution I have seen, when I add R=301 into the rule it redirects the nice url to the long version.
    Code:
    RewriteEngine On
    
    RewriteRule ^([^/\.]+)/$ /index.php?page=$1 [L]
    So in short the two problems I have with this are:
    1. It doesn't actually redirect the URL, it just allows both version to work.
    2. If I tried to use the new url without the trailing backslash
    HTML Code:
    www.example.com/animals
    it will just bring up the 404 page. It should redirect to the page with a trailing slash.

    Any help will be much appreciated
    Many thanks

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    g-85,

    What've you attempted was not a bad effort but I'd REALLY advise not mimicing directories (trailing /'s) as that introduces an incorrect directory level in the path for your relative links.

    If you're assured that your "animals" will always be lowercase characters, I'd use
    Code:
    RewriteEngine on
    RewriteRule ^([a-z]+)$ index.php?page=$1 [L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •