Solved

301 redirect from URL with subdirectories

Posted on 2011-02-22
8
441 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

8 Experts available now in Live!

Get 1:1 Help Now