[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

301 redirect from URL with subdirectories

Posted on 2011-02-22
8
Medium Priority
?
449 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
Introduction This warning has to be one of the most commonly issued warnings in the history of PHP.  The article explains why this warning arises and what to do to mitigate the problem. How this Happens HTTP headers include many different kinds…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

650 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