I'm having this very annoying problem with my rewrite rules in the .htaccess
file.
The context
So what I want is to have these two types of URLs rewrite to different targets:
URL 1 -- http://example.com/rem/call/answer/{Hex String}/{Hex String}/
URL 2 -- http://example.com/answer/{Hex String}/{Hex String}/
This is an extract of my .htaccess file:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
The problem
Now the problem is that URL 2 rewrites well (using rule #2) and goes to TARGET 2, but URL 1 rewrites with both rules instead of just rule #1.
I tried several solutions, including the obvious use of the character ^
for "start of string". At that point, my rewrite rules were:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1
RewriteRule ^answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
However, another problem happened. This time it's URL 1 that rewrites well, with only rule #1 and goes to TARGET 1. But now URL 2 doesn't rewrite at all any more. I'm guessing it's because the second rewrite rule never matches any url and thus never applies.
The only solution I found so far is to remove the ^
and use the [L]
flag at the end of rule #1 like so:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1 [L]
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
This way, it uses rule #1, matches, but never gets to rule #2. Both urls get rewritten properly with these rules, but it is not a good solution since I might not want to stop the rewriting of URL 1 after the first rule applies (what if I have a third rule I would want to apply to it as well...)
My questions to you
Now that I've stated the problem, my questions here are:
- Is the
[L]
flag the only way to go ? (which I highly doubt, and certainly hope not) - Would
^
be a candidate solution ? (I think so) - If so, how to make it work and why is it not working at all in my case ?
What I suspect
I suspect that it has something to do with the fact that the URL is actually http://example.com/answer/{Hex String}/{Hex String}/
and not just answer/{Hex String}/{Hex String}/
, which means that answer/..
isn't really at the beginning of the string and thus prefixing it with ^
doesn't work.
But then it brings me to another question:
How to tell apache to strip the url of the scheme+domain part (i.e. http://example.com/
) and match rules with the remainder of the url only (e.g. answer/{Hex String}/{Hex String}/
) ?
EDIT
I should also add that I've tried the basic alice-bob example. I have a file named bob.html in my root and the following rule in my .htaccess
file:
RewriteRule alice.html$ /bob.html
This works just fine and displays the bob.html page when alice.html is queried. However, if I change the rule to:
RewriteRule ^alice.html$ /bob.html
I then get a 404 error when querying the alice.html page...
As for @anubhava's comment, my full .htaccess file is composed as follows:
RewriteEngine On
[A bunch of RewriteRule that have nothing to do with the topic at hand
(don't contain any "answer" string in them and all work perfectly)]
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1 [L]
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
ErrorDocument 404 /404.html
Header set Access-Control-Allow-Origin "*"
SetEnv file_uploads On