Solved

301 redirect from URL with subdirectories

Posted on 2011-02-22
8
442 Views
Last Modified: 2012-05-11
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.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=62&l=158$ [NC]
RewriteRule ^plan\.php?$ http://www.domain.com/visitor/int/dev.html [R=301,L]

Open in new window

0
Comment
Question by:Jonathan Greenberg
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 34961428
>>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


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.
0
 

Author Comment

by:Jonathan Greenberg
ID: 34962742
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.
0
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
ID: 34963572
Try

RewriteRule ^(.*)/plan\.php$  http://www.domain.com/visitor/int/dev.html [R=301,L]

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:Jonathan Greenberg
ID: 34963700
Yes, that does it. Thanks!
0
 
LVL 27

Expert Comment

by:BigRat
ID: 34963716
That in fact just ignores sub directories, if you want something more finer, just post here and I'll add it for free.
0
 

Author Comment

by:Jonathan Greenberg
ID: 34963794
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?
0
 
LVL 27

Expert Comment

by:BigRat
ID: 34963837
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.
0
 

Author Comment

by:Jonathan Greenberg
ID: 34963902
Thanks so much! That's very helpful. I really appreciate it.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
file path 14 72
how to use external config file with Spring MVC 4 158
Setting up a WAMP server... 7 68
Internal Server Error on 1&1 Apache Server running PERL 3 71
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction One of the frequent application design questions goes something like this: "How can I confirm when a client registers on my web site?" The registration might be for general use of a self-administered site like a forum, or for attend…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question