Welcome to WebmasterWorld Guest from 23.22.220.37

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect not working correctly when trailing slash is left off url

     
2:24 am on Aug 1, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 7, 2005
posts:112
votes: 0


Hi

In the course of investigating a seperate problem i found my redirect isn't working correctly if the user doesn't add a trailing slash to the url they are entering

basically i am redirecting /phpBB3 to /forum

eg
example.com/phpBB3/ redirects BUT
example.com/phpBB3 doesn't


RewriteRule ^phpBB3/(.*)$ http://example.com/forum/$1 [L,R=301]



My rule is shown above, and i can't seem to fis it without breaking the functionality of the above rule, Its a really minor thing (according to my stats there's only been 20 404s because of it) its just bugging me as It should work
3:02 am on Aug 2, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Make the slash optional, as well as anything that might follow:

RewriteRule ^phpBB3(/(.*))?$ http://example.com/forum/$2 [L,R=301]

Jim
1:38 am on Aug 3, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 7, 2005
posts:112
votes: 0


Thanks for that Jim

just one question why are you using $2 rather than $1
1:51 pm on Aug 3, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


To avoid duplicating the slash if it is present in the requested URL-path. Using $1, if example.com/phpBB3/something was requested, you would end up with example.com/forum//something.

An alternative would be to use two rules, one for "example.com/phpBB3" requests, and another for "example.com/phpBB3/<anything or nothing>" requests, but that would be rather a waste of bytes and CPU time.

Jim