homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect not working correctly when trailing slash is left off url

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


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

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)

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

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



 1:38 am on Aug 3, 2010 (gmt 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)

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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved