Solved

301 redirect from URL with subdirectories

Posted on 2011-02-22
8
438 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
 

Author Comment

by:Jonathan Greenberg
ID: 34963700
Yes, that does it. Thanks!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IBM HTTP Server Log rotation 5 127
Magento error 9 67
MDB2 Error: not found 4 135
Use of SSL Certificates - http vs https 3 61
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now