homepage Welcome to WebmasterWorld Guest from 54.161.220.160
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteCond used more than once but only works for one instance?
rewritecond rewriterule
hottrout



 
Msg#: 4548662 posted 3:07 pm on Feb 25, 2013 (gmt 0)

The subject does not properly explain what my problem is. I had received some excellent help from Lucy a few months back in writing the following code

# Redirect to remove hex-encoded query string characters from DOGS folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\%3[fF]folder\%3[dD]([^\ ]+)\ HTTP/
RewriteRule ^dogs/index\.php http://www.example.com/dogs/index.php?folder=%2 [R=301,L]


This is the link to the thread where we ended up with the code being created. [webmasterworld.com...]


The purpose of this code (which is working) is to capture any reference in the form index.php%3Ffolder%3D and change it to index.php?folder=

This works for all references to the dogs folder. I then also wanted this to work for the CATS and BIRDS folders as well. I used the same format to create another similar RewriteCond as follows :-

# Redirect to remove hex-encoded query string characters from CATS folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\%3[fF]folder\%3[dD]([^\ ]+)\ HTTP/
RewriteRule ^cats/index\.php http://www.example.com/cats/index.php?folder=%2 [R=301,L]

However, although the DOGS works, the CATS and BIRDS version does not? Within my HTACCESS the DOGS rerwite came first so I decided to swap the CATS rewrite to before the DOGS rewrite to see if that made a difference but it did not.

I also experimented with this code which I though should work and also simplify the amount of code, but this also did not work except for DOGS :-

# Redirect to remove hex-encoded query string characters from DOGS, CATS and BIRDS folders
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\%3[fF]folder\%3[dD]([^\ ]+)\ HTTP/
RewriteRule ^cats/index\.php http://www.example.com/cats/index.php?folder=%2 [R=301,L]
RewriteRule ^dogs/index\.php http://www.example.com/dogs/index.php?folder=%2 [R=301,L]
RewriteRule ^birds/index\.php http://www.example.com/birds/index.php?folder=%2 [R=301,L]

I am at loss as to why DOGS will work but CATS will not. Can anyone shed some light on this for me? Please remember that I am not using the second simplified code now but an still trying to get the three seperate RewriteCond and RewwriteRule commands working.

 

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 10:58 pm on Feb 25, 2013 (gmt 0)

I haven't looked over the rest of it, but your simplified version won't work.

You can have many RewriteCond for a single RewriteRule, but only one RewriteRule for a set of RewriteCond.

So you have to simplify more and match (cats|dogs|birds) and replace with $1 in a single rule

hottrout



 
Msg#: 4548662 posted 4:08 pm on Feb 27, 2013 (gmt 0)

I am not using the second version simplified code, I already worked out that it would not work. It is the first example of code that I need help understanding why it will not work.

Anyone have any ideas.

Andy Langton

WebmasterWorld Senior Member andy_langton us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 4:17 pm on Feb 27, 2013 (gmt 0)

There's nothing wrong with the code. I'd suggest it's either a caching issue (try checking with an external HTTP header checker) or there is some other rule interfering with this one.

Also try using simplifed code as suggested by ergophobe:


# Redirect to remove hex-encoded query string characters from animal folders
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\%3[fF]folder\%3[dD]([^\ ]+)\ HTTP/
RewriteRule ^(cats|dogs|birds)/index\.php http://www.example.com/$1/index.php?folder=%2 [R=301,L]


If dogs redirects but birds doesn't you know it is not a code problem.

hottrout



 
Msg#: 4548662 posted 6:15 pm on Feb 27, 2013 (gmt 0)

OK thank you. I chanegd my code to the following,

# Redirect to remove hex-encoded query string characters from animal folders
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\%3[fF]folder\%3[dD]([^\ ]+)\ HTTP/
RewriteRule ^(cats|dogs|birds)/index\.php http://www.example.com/$1/index.php?folder=%2 [R=301,L]

It works for dogs and birds but not cats. Any ideas what I should be looking for that would cause the others to not function?

Andy Langton

WebmasterWorld Senior Member andy_langton us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 6:21 pm on Feb 27, 2013 (gmt 0)

Do you have an htaccess file in the cats directory?

hottrout



 
Msg#: 4548662 posted 6:33 pm on Feb 27, 2013 (gmt 0)

Good grief. I have an htaccess in that folder but there are no commands in it. I removed it anyway and guess what it is working.

Slaps Head

Why did it effect the command? Did it void the previous htaccess requests?

Thank you for this, I had been tearing my hair out over this.

hottrout



 
Msg#: 4548662 posted 6:39 pm on Feb 27, 2013 (gmt 0)

BTW, this also fixes another issue I had that made no sense. Thank you so much. I feel like an ass. No matter how much I study regx I still get totally lost sometimes.

Andy Langton

WebmasterWorld Senior Member andy_langton us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 7:03 pm on Feb 27, 2013 (gmt 0)

I believe that unless RewriteOptions inherit is enabled, the second file would override the first, in effect causing there to be no rules to evaluate. But I wouldn't be able to say for sure as I'm no Apache expert by any means. Glad you got it working, regardless! :)

hottrout



 
Msg#: 4548662 posted 7:10 pm on Feb 27, 2013 (gmt 0)

Many thanks Andy, that really saved me from loosing my mind.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4548662 posted 1:31 am on Feb 28, 2013 (gmt 0)

Good grief. I have an htaccess in that folder but there are no commands in it.

Ooh, good call, Andy. I discovered something similar just recently in a completely unrelated experiment. An empty htaccess file in a subdirectory prevented RewriteRules in the higher directory from working.

I detoured here to try to reproduce the problem. After much experiment, I think I nailed it.

If an htaccess file contains the RewriteEngine On directive and/or any RewriteRules-- even ones that won't work because the Engine directive is missing-- then RewriteRules in higher-level htaccess files will no longer work for the directory containing the inner RewriteRules. This seems backward, but who's going to argue with Apache?

Other permutations have no effect. The ones I tried were: completely empty htaccess; htaccess with content (line breaks) but no actual rules; htaccess containing rules unrelated to mod_rewrite; htaccess in a different subdirectory.

Unlike most settings in Apache, the RewriteEngine has to be turned on in each separate htaccess-- and even in Files(Match) envelopes within a single htaccess.

:: detour to Apache docs ::

Holy ###. I never knew the RewriteOptions directive even existed. I thought you were talking about some newfangled 2.4 stuff, but there it is. There are three options, all of them off by default.

Rules inherited from the parent scope are applied after rules specified in the child scope.


Oh, no wonder it's off by default. That means the whole thing has to run backward: you can't start any rewriting until you've arrived at the directory containing the requested file.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 2:03 am on Feb 28, 2013 (gmt 0)

This stuff has me boggled too -- hence the oft repeated "put all your rules in the root htaccess file and get the rules in the right order".

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 2:25 am on Feb 28, 2013 (gmt 0)

Lucy24, this surprises me. As long as the rules don't conflict, the inherited rules should get applied (that's my vague understanding - I've never actually used this).

For example, if you have
^(.*)$ index.php?q=$1 [L]

in a virtual host context, that will match all URLs and so no inherited rules can get applied.

I thought you were talking about some newfangled 2.4 stuff, but there it is.


You prompted to do a search of WebmasterWorld and it comes up with surprising frequency:

- Interesting note from 2004: [webmasterworld.com...]
- And Jim's stock advice for certain problems: "double check your RewriteOptions settings"
* [webmasterworld.com...]
* [webmasterworld.com...]
* [webmasterworld.com...]

Even more surprising, given your surprise, is that Lucy24 posted that same quote back in 2011! - [webmasterworld.com...]

:-)

And many more https://www.google.com/search?num=100&hl=en&q=site%3Awebmasterworld.com+"rewriteoptions+inherit"

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4548662 posted 5:36 am on Feb 28, 2013 (gmt 0)

Even more surprising, given your surprise, is that Lucy24 posted that same quote back in 2011!

This does not surprise me in the least :( I do note that I felt constrained to say "Do as I say, not as I do."

I currently have multiple htaccess files but it's as close as you can get to an unimpeachable reason: multiple domains in one userspace. Some directives have to go within individual domains-- or lower-- because they won't work at a higher level. But if I left out the shared htaccess I'd have to maintain three identical IP/UA lists. So I've compromised by saying that any given module can happen only at one level or the other.

And, as always, Options +Indexes or -Indexes doesn't count.

hottrout



 
Msg#: 4548662 posted 9:11 am on Feb 28, 2013 (gmt 0)

Thank you Lucy for bringing some further clarity to this. I swear, I have learnt a lot from you guys over the past two years but this was making me question everything I had come to understand. It is great to find out you have not lost your mind.

I have removed the other htaccess and think I will just stick to one htaccess in the root. Ironically I only created the subfolder htaccess to test and never really actually used it.

ergophobe

WebmasterWorld Administrator ergophobe us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 10:30 pm on Feb 28, 2013 (gmt 0)

This does not surprise me in the least


No, I didn't think it would. If you've been around here long enough you've had the experience of googling to see how to fix some problem, landing on a thread here, and realizing that once upon a time you actually answered that same question for someone else.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4548662 posted 10:38 pm on Feb 28, 2013 (gmt 0)

I'm not alone in that then. That's good to know. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved