mod rewrite - redirect problem

Discussion in 'Apache' started by debbie-g85, Dec 28, 2011.

  1. #1
    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
    www.example.com/index.php?page=animals
    Code (markup):
    to this (including a trailing backslash)
    www.example.com/animals/
    Code (markup):
    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.

    
    
    RewriteEngine On
    
    RewriteRule ^([^/\.]+)/$ /index.php?page=$1 [L]
    
    
    Code (markup):
    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

    www.example.com/animals
    Code (markup):
    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
     
    debbie-g85, Dec 28, 2011 IP
  2. kokopelli

    kokopelli Peon

    Messages:
    2,436
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    kokopelli, Dec 28, 2011 IP
  3. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #3
    Try maybe something like this:
    
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
    
    Code (markup):
    The question mark between /?$ will fix the trailing trash problem. Both url's will work, yes.
     
    pr0t0n, Dec 29, 2011 IP