?
Solved

301 redirect from URL with subdirectories

Posted on 2011-02-22
8
Medium Priority
?
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 34963572
Try

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

0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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