Jonathan Greenberg
asked on
301 redirect from URL with subdirectories
I'm using 301 redirects on my htaccess page, but I can't figure out how to redirect URLs that contain subdirectories.
The code below redirects this URL
http://www.domain.com/plan.php?s=62&l=158
to this URL
http://www.domain.com/visitor/int/dev.html
How do I modify this code to perform the same redirection from this URL:
http://www.domain.com/_sub1/_sub2/plan.php?s=62&l=158
Thanks.
The code below redirects this URL
http://www.domain.com/plan.php?s=62&l=158
to this URL
http://www.domain.com/visitor/int/dev.html
How do I modify this code to perform the same redirection from this URL:
http://www.domain.com/_sub1/_sub2/plan.php?s=62&l=158
Thanks.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=62&l=158$ [NC]
RewriteRule ^plan\.php?$ http://www.domain.com/visitor/int/dev.html [R=301,L]
ASKER
BigRat, thanks for trying to help, but that actually doesn't work at all, on the original URL or the one with subdirectories.
On the RewriteRule line, if I eliminate the '/' before 'plan', then it correctly redirects the original URL. But it still doesn't redirect the URL with the subdirectories.
On the RewriteRule line, if I eliminate the '/' before 'plan', then it correctly redirects the original URL. But it still doesn't redirect the URL with the subdirectories.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that does it. Thanks!
That in fact just ignores sub directories, if you want something more finer, just post here and I'll add it for free.
ASKER
Thanks, that's very kind. Yes, I'd appreciate that.
But my results are a little different than what you've explained. Your new line of code doesn't completely ignore directories. It doesn't care what the directories are named, but it requires the URL to contain at least 1 directory. To make both my original URL and the new one with subdirectories redirect, I now need to use both lines of code: my original one and your new one.
Is that not how it should work?
But my results are a little different than what you've explained. Your new line of code doesn't completely ignore directories. It doesn't care what the directories are named, but it requires the URL to contain at least 1 directory. To make both my original URL and the new one with subdirectories redirect, I now need to use both lines of code: my original one and your new one.
Is that not how it should work?
Well, yes, sort of. I usually do not try to pack all my regular expressions together to reduce the number of rewrite rules. In fact Apache compiles then at start up so they run very quickly.
This rule
RewriteRule ^(.*)/plan\.php$ http://www.domain.com/visitor/int/dev.html [R=301,L]
expects the / to be there before "plan". This rule makes it optional
RewriteRule ^(.*)/?plan\.php$ http://www.domain.com/visitor/int/dev.html [R=301,L]
The question behind the slash makes it optional
Look at http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule there's a nice box with the meta characters defined.
This rule
RewriteRule ^(.*)/plan\.php$ http://www.domain.com/visitor/int/dev.html [R=301,L]
expects the / to be there before "plan". This rule makes it optional
RewriteRule ^(.*)/?plan\.php$ http://www.domain.com/visitor/int/dev.html [R=301,L]
The question behind the slash makes it optional
Look at http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule there's a nice box with the meta characters defined.
ASKER
Thanks so much! That's very helpful. I really appreciate it.
http://www.domain.com/_sub1/_sub2/plan.php?s=62&l=158
Well supposing that the URL always ends in plan.php then
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=62&l=158$ [NC]
RewriteRule (.*)/plan\.php?$ http://www.domain.com/visitor/int/dev.html [R=301,L]
redirects ANY URL ending with plan.php to the visitor page.