Forum Moderators: phranque
I have written a rule to redirect any calls to one of my domains to redirect to another domain unless a specific folder(s) are called, in which case it should not redirect.
Due to the 2 domains both working off the same root folder I have put a condition on the host header to make sure that the rule only works on the specific domain.
On my testing server it works correctly and redirects (i comment out the host header condition) but when I put it live it doesn't work.
Here is the rule.
# redirect all .co.uk requests to .com except the exclusions
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$
RewriteCond %{REQUEST_URI} !^/(softwareŠinclude)(/?Š/.*)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
The rule was working, but caused some redirect loops on the .com domain and now it no longer redirects in the live environment.
To test that the htaccess file was in fact working, I added some additional rules which work fine, so I have eliminated the possibility of htaccess not working.
Also as mentioned, if I comment out the first rule and test on my local server it works perfectly.
Any ideas?
Thanks
However, better-informed opinions are *always* welcome here, and if "making the regex library decide" does result in slower execution, I'll happily go back and re-anchor all my ".*" patterns.
As should be obvious, the vast majority of Webmasters reading here are limited to .htaccess context on name-based shared servers. So that is our usual "default context." Where differences exist between the per-dir context and the server-config context, we do like to try to make those differences explicit in the discussion.
Jim
The regEx is automatically anchored by PCRE if
- .* was used and the regEx is compiled with PCRE_DOTALL and, if it's capturing, there's no backreference like \1 within the pattern
- ^ was used and the regEx is not compiled with PCRE_MULTILINE
and in some other cases.
Benchmarking is not easy... Some time ago I ran apache bench three times one after another (without changing anything) and the results differed what I'd call out of tolerance so comparing the result with a modification which doesn't lead to a massive difference seems to be difficult.
I wouldn't expect too much especially on fast machines but that doesn't mean that I'd remove ^ explicitly. The regEx is not compiled with PCRE_MULTILINE, so PCRE would set PCRE_ANCHORED.
ap_regcomp() does not call pcre_compile() with the option PCRE_DOTALL, i.e. you shouldn't get the optimization unless you anchor the pattern with ^ explicitly. I don't know what optimization POSIX extended (apache 1.3.x) does, but that engine is known to be slower than PCRE.
The execution (per-directory) of the merged directives by its corresponding modules (mod_rewrite -> fixup-phase) occurs at a later stage of processing.
If the request was denied (access_check-phase) the regEx from the .htaccess file was compiled but never executed since access_check runs prior fixups.
[edited by: Caterham at 7:01 pm (utc) on Mar. 15, 2009]
At runtime, the module gets its merged configuration and can't distinguish if parts of the configuration were originally defined by directives in a .htaccess file or in a <Directory> section.