Trailing Slash Redirect Problem

Discussion in 'Apache' started by ronin90, Mar 21, 2008.

  1. #1
    Ok, so I'm sure you've all heard this problem loads of times, but I have search and searched and not found an example which can directly solve my problem.

    I am currently re-writing the following

    http://www.domain.com/?p=variable

    to

    http://www.domain.com/variable/

    However, if I typed in http://www.domain.com/variable (without the trailing slash) I was shown a nice 404 page. I have now fixed that so that I no longer get a 404 page, but the current solution I have in place creates a canonical URL, since http://www.domain.com/variable and http://www.domain.com/variable/ are two different URLs.

    Is it possible to redirect the http://www.domain.com/variable URL to http://www.domain.com/variable/ ?

    My .htaccess is currently as follows for this.

    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*[^/])/?$ /?p=$1 [L]

    Thanks in advance,
    Ronin90.
     
    ronin90, Mar 21, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You could try something like this:

    
    RewriteEngine On
    RewriteRule ^([a-z]+)/?$ index.php?p=$1
    
    Code (markup):
    Untested, off the top of my head.

    Jay
     
    jayshah, Mar 22, 2008 IP
  3. ronin90

    ronin90 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Would that be instead of RewriteRule ^(.*[^/])/?$ /?p=$1 [L] or as well as?

    If possible I'd like to do a 301 from without a trailing slash, to with a trailing slash.

    http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

    It's mentioned there under "Trailing Slash Problem", but they only do it with static URLs, not URLs that have already been re-written.
     
    ronin90, Mar 22, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Hello,

    The suggestion (rules) that I provided would replace all you current rules.

    If you wanted to do a 301, you'd have to have two rules. The first would capture a slash, and the second would capture it without, then send a HTTP 301 to redirect the user to the right location. Lookup the extra RewriteRules such as R,L.

    Jay
     
    jayshah, Mar 22, 2008 IP
  5. ronin90

    ronin90 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried putting your rule in place, and it didn't work.

    Do you know how to write the R=301 rule to redirect from without slash to with slash?

    I currently have...

    RewriteCond %{HTTP_HOST} ^(www.)?domain\.com$ [NC]
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

    In place, so that if someone types in the domain without a trailing slash, it adds one. I don't know how to translate that to this scenario though.
     
    ronin90, Mar 22, 2008 IP
  6. ronin90

    ronin90 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Don't worry, I resolved the problem using PHP.
     
    ronin90, Mar 22, 2008 IP
  7. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just to follow this up with a pure Apache solution, the problem is that your original regex treats two different URLs the same and this is the part that does it:
    RewriteRule ^(.*[^/])[B][COLOR="Red"]/?[/COLOR][/B]$ /?p=$1 [L]
    Code (markup):
    The rule above does the same thing to URLs that have or don't have the trailing slash. What you need to do is split that part out into it's own RewriteRule and put the 301 redirect on that rule.
    RewriteRule ^(.*[^/])$ /?p=$1 [L]
    RewriteRule ^(.*[^/])/?$ /$1 [R=301,L]
    Code (markup):
     
    Ladadadada, Mar 23, 2008 IP