Forum Moderators: phranque

Message Too Old, No Replies

htaccess: Problem with conditional redirect for subsubdirectories

         

nandor

3:18 am on Sep 5, 2007 (gmt 0)

10+ Year Member



Hello!

I would like to do the following in my .htaccess file:

REDIRECT http://example.com/yyy/zzz... TO http://example.com/
EXCEPT IF yyy = documents

I have tried some variants on

RedirectMatch permanent /!(documents)/* http://example.com/

but it doesn't work. Any suggestions?

Please note that I do NOT want to redirect anything of the form http://example.com/yyy or http://example.com/yyy/ -- i.e., I only want to redirect if there is something after that next slash.

thanks so much!

[edited by: jdMorgan at 3:36 am (utc) on Sep. 5, 2007]
[edit reason] example.com [/edit]

jdMorgan

3:49 am on Sep 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need mod_rewrite to do this, since you need an exclusion;

Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect /dir1/<something> to example.com/ unless dir1=documents
RewriteCond %{REQUEST_URI} !^/documents/
RewriteRule ^[^/]+/. http://example.com/ [R=301,L]

Note that neither pattern is end-anchored. This is intentional.

If you don't have any other working mod_rewrite code, you will need either the second line above (RewriteEngine), or both the first (Options) and the second line. Try it with only the second line, and add the first if you get a server error. If you've already got other working rules, then you'll already have one or both of those lines in the file, and there is no need to repeat them.

Jim

g1smd

1:14 pm on Sep 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you also have set up non-www to www canonicalisation, then the code above must go BEFORE your existing code as it is more specific.

nandor

12:21 am on Sep 6, 2007 (gmt 0)

10+ Year Member



Hello jdMorgan and Jim,

Thanks so much for your comments -- especially yours, jdMorgan, as I appreciated your explanation.

Unfortunately, this is conflicting with a couple other rules I've made up. My complete htaccess file is the following:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /base/htdocs
RewriteCond %{REQUEST_URI}!^/documents/$ [NC]
RewriteRule ^[^/]+/. http://example.com/ [R=301,L] # (1)
RewriteRule ^([^/\.]+)/?$ /mediawiki/index.php/$1 [NC,L] # (2)
RewriteRule ^/?$ /mediawiki/index.php/startPage [NC,L] # (3)

(1) is your solution
(2) maps example.com/yyy to example.com/mediawiki/index.php/yyy
(3) maps example.com/ to example.com/mediawiki/index.php/startPage

The problem is that all three together cause http://example.com/ to go through an infinite loop and never stop at any one website.

Any ideas what is happening? I'll admit that I don't quite understand (2), which was a suggestion from someone else.

Thanks so much!

g1smd

1:12 am on Sep 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I always place a RewriteCond condition before each and every rule to separate out exactly when that rule will run to avoid such loops.

I am not following the logic closely enough to suggest what you should test here (REQUEST_URI, HTTP_HOST, THE_REQUEST, or whatever) nor for what values.

jdMorgan

1:36 am on Sep 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because index.php is not located in /documents, it will be redirected to "/" by rule #1, as you requested.
Then your third rule rewrites "/" to index.php, which causes a loop.

You need to be careful and fully-specify your requirements before coding...

Jim

[edited by: jdMorgan at 1:37 am (utc) on Sep. 6, 2007]

nandor

2:44 am on Sep 6, 2007 (gmt 0)

10+ Year Member



Thanks again you two! I actually did fully specify the requirements to myself ... but in the process, I didn't realize that those requirements created a loop, as manifested above. Thank you so much for pointing this out to me.

gratefully,

NATE

g1smd

1:18 pm on Sep 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



..