homepage Welcome to WebmasterWorld Guest from
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

Cant get RewriteCond to capture URL
rewritecond, rewriterule, problems capturing url

 5:13 pm on Feb 5, 2013 (gmt 0)

I have dony many Rewritecond rules before but for the life of me I can not figure out why this one will not trigger.

The url I want to catch is this

http://www.example.com/files/index.php?folder=M/IPHONE/(what ever comes next)

I want to 301 it to

http://www.example.com/files/index.php?folder=M/IOS/(what ever comes next)

The code I am using is

RewriteCond %{THE_REQUEST} ^files/index\.php\?folder\=M/IPHONE([^\ ]+)\ HTTP/

RewriteRule ^files/index\.php http://www.example.com/files/index.php?folder=M/IOS/%1 [R=301,NC,L]


I am most likely doing something very stupid but for the life of me I can't see it. I have been looking at it too long to be honest.



 12:03 am on Feb 6, 2013 (gmt 0)

RewriteCond %{THE_REQUEST} ^files/

THE_REQUEST is the full request string beginning with GET (or similar), space, directory-slash ... and then the part you've got.

This is the part where you kick yourself. Been there. Done that.

The condition will only be avaluated if the user asks for "index.php" by name. The rule itself will fail-- and the condition won't be evaluated at all-- if they ask for files/ alone. Unless files/ is a real, physical directory; if so, the Rule may kick in on the second pass, after mod_dir has done its thing. But you probably want (index\.php)?$ to be safe.


 12:06 am on Feb 6, 2013 (gmt 0)

What Lucy said, plus: the part...

%{THE_REQUEST} ^files

should be

%{THE_REQUEST} ^[A-Z]{3,9}\ /files


 7:38 pm on Feb 6, 2013 (gmt 0)

Slaps forehead, makes changes but.... no joy and my code now looks like this. Any ideas?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /files/index\.php\?folder\=M/IPHONE([^\ ]+)\ HTTP/

RewriteRule ^files/index\.php http://www.example.com/files/index.php?folder=M/IOS/%1 [R=301,NC,L]


 7:43 pm on Feb 6, 2013 (gmt 0)

Does it return a redirect and the new URL fail to result in content, or does it fail to redirect?

The Live HTTP Headers extension for Firefox might be useful.

The = in the pattern does not need to be escaped.

Adding ( and )? round the index\.php (and adding $ to the one in the rule) is also a good idea.


 7:51 pm on Feb 6, 2013 (gmt 0)

It just does not get caught. If I enter the url




It just stays at the URL with no content.


 8:13 pm on Feb 6, 2013 (gmt 0)

Just to be sure, the code is located in the root htaccess file, not in a folder?


 8:18 pm on Feb 6, 2013 (gmt 0)

It is in the root. This is my code now.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /files/index\.php\?folder=M/IPHONE([^\ ]+)\ HTTP/

RewriteRule ^files/index\.php http://www.example.com/files/index.php?folder=M/IOS/%1 [R=301,NC,L]


 8:41 pm on Feb 6, 2013 (gmt 0)

I change the code to this just to see but it also did not catch and redirect. What gives?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([^/]+/)*index\.php\?folder=M/IPHONE([^\ ]+)\ HTTP/

RewriteRule ^files/index\.php http://www.example.com/files/index.php?folder=M/IOS/%1 [R=301,NC,L]


 11:53 pm on Feb 6, 2013 (gmt 0)

:: jumping up and down excitedly ::

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([^/]+/)*index\.php\?folder=M/IPHONE([^\ ]+)\ HTTP/

RewriteRule ^files/index\.php http://www.example.com/files/index.php?folder=M/IOS
/%1 [R=301,NC,L]

I note with interest that the Forums will only honor "size=" if it comes inside the "red" tags. Imagine that.

Then again, this may have nothing to do with the rule's failure to execute, and will only kick in after you've solved some other, unrelated problem. Never mind the screen content: What does your address bar say when you try the rule?


 12:33 am on Feb 7, 2013 (gmt 0)

The address bar does not change at all. I forgot to change to %2 after adding the ([^/]+/)*

Any other ideas?


 12:44 am on Feb 7, 2013 (gmt 0)

Slash isn't supposed to be a valid character within query string parameters.

It's reserved as a folder separator within the path part of a URL.

Is the slash arriving in an encoded form?

In a related question, why does the new URL look like
and not something like this


 1:52 am on Feb 7, 2013 (gmt 0)

! And I wasn't even thinking about that aspect of the slash issue, just noting that there are more of them on one side than the other.

hottrout, the "related question" above isn't about the code as written, it's a philosophical question: As long as you're redirecting, why aren't you redirecting to a prettier query-less URL? (I wondered the same thing myself but sat on my hands ;))


 9:06 am on Feb 7, 2013 (gmt 0)

At the minute my PHP code generates the URLs for folder= based on the actual folder structure under that subfolder. This is something that I plan on changing in the near future so that visable URLs are devoid of the parameters. Until I rewrite the PHP code this has to be the case.

I wanted to redirect the traffic going to folder=M/IPHONE to the new folder folder=M/IOS in the meantime so as to not loose visitors that are arriving at the old folder name and expwecting content.

Is there no way to do this with / in the parameter?


 11:48 am on Feb 7, 2013 (gmt 0)

It's iffy. Can't you use some other character and let your php function convert it back into a directory slash? Or don't include that part in the query string at all, since it's simply m/something vs. m/something-else.


 11:54 am on Feb 7, 2013 (gmt 0)

Really what I want to do is change




Is this possible?


 12:12 pm on Feb 7, 2013 (gmt 0)

Try a simpler redirect using
...\?folder=M([^\ ]+)\ HTTP/ without a slash in your pattern (where "..." represents existing code that you don't alter), and redirect to example.com/?target=M%1 and see what the slash looks like in the address bar of the browser after the redirect. Don't worry that the redirected URL returns 404, you're only interested in what the address bar changes to. Redirect to a completely different hostname if you want, you're only interested in what the new URL "looks" like.

[edit: fix typo]

[edited by: g1smd at 12:45 pm (utc) on Feb 7, 2013]


 12:21 pm on Feb 7, 2013 (gmt 0)

It returned a 500 server error


 11:58 pm on Feb 7, 2013 (gmt 0)

That sounds like a typo. But it's a basic mod_rewrite test: If you change the rewrite into a redirect, you will see the result in your browser's address bar. Then you can see if (1) the redirect/rewrite is taking place at all, and (2) it's going to where it's supposed to go. When there are php scripts involved, you may need to change some other detail of the target as well, so the script can't do anything to it.

Again, all this is purely for experimentation. If you haven't already got MAMP/WAMP or linux equivalent (uh, it isn't called LAMP is it?) install it. It will not always be identical to your live setup, but it is definitely useful.

A 500-class error should show up in your error logs. Go have a look.


 4:32 pm on Feb 28, 2013 (gmt 0)

Guess what guys. This problem was indeed also caused by the same htaccess file being unused but present in the folder. Just wanted to update you all that this hair pulling problem was down to the same thing.

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