Forum Moderators: phranque
RewriteRule ^/(.*)$ [servername:8085...] [P,L,NC]
as part of new functionality.
We also have many redirectmatches in a seperate file and they are not working because of this new rewrite rule.
Below are the new changes that have been added to httpd.conf file
RewriteCond %{REQUEST_URI} !^/#*$!
RewriteCond %{REQUEST_URI} !^/yyy
RewriteCond %{REQUEST_URI} !^/zzz/
RewriteRule ^/(.*)$ [colhpawebCS04:8085...] [P,L,NC]
redirects.conf
RedirectMatch ^/haveyoursay/?$ /HPAweb_C/1204186206277
is their any way to tell apache not to apply rewrite rule for redirectmatches?
In this case you do have the rule in your httpd.conf file and the leading slash is required. What 'container' is this directive in?
Be aware that since (.*) matches everything, anything, and nothing, that none of the following rules will be parsed. That will explain why other rules are 'not working'.
Those rules will have to be invoked before this one.
Also, do be aware not to mix rules from mod_rewrite and mod_alias. You may get unintended operation. Rules are evaluated in per-module order, and NOT in the raw order they are presented in the configuration file.
I typed it wrong. Below are the contents of my httpd.conf file under virtual host in apache.
<virtualhost:80>
----
---
RewriteCond %{REQUEST_URI} !^/#*$!
RewriteCond %{REQUEST_URI} !^/yyy
RewriteCond %{REQUEST_URI} !^/zzz
RewriteRule ^/(.*)$ [servername:8085...] [P,L,NC]
Include conf/redirects.conf
</virtualhost>
Contents of redirects.conf file
RedirectMatch (?i)^/test.*$ /test/AboutThetest/
...there are hundreds of redirectmatches like above.
I have tried placing include conf/redirects.conf tag above the rules but redirectmatches are still not working
Basically rewrite rule is being applied instead of redirectmatch. Any clues how to exclude redirectmatches?
[edited by: job_ppa at 6:42 pm (utc) on Dec. 3, 2009]
You can then control rule execution order and precedence by the proper ordering of your rules and the use of the [L] flag.
Jim
Does it mean that redirectmatch and rewriterule directives can't co-exist if the regular expressions clash?
Changing hundreds of redirectmatches to rewrite rules is a daunting task...but im prepared to do it only if their is no alternative...
Thanks for your help
Since you need to have some rewrites, and since rewrites can only be coded using RewriteRule from mod_rewrite, all of your rules need to use code from mod_rewrite. RedirectMatch is invoked by a different module; mod_alias.
You have to use one module or the other. In this case (and in most cases) mod_rewrite is the one that people will end up using.
The syntax changes are minor. Be sure that any redirect is set as a 301, that every rule has an L flag, and that all redirects include the protocol and domain name in the target.
Test a couple of specific redirects first to make sure the syntax is correct. Mod_rewrite is a lot more powerful than RedirectMatch, and you might find that many of your rules can be combined by using RewriteRule.
The alternative to replacing those RedirectMatches with RewriteRules is to add exclusions to your existing RewriteRule, adding a negative-Match RewriteCond (as you have already done for xyz, yyy, and zzz above) for each URL currently redirected by RedirectMatch.
So you can avoid changing hundreds of RedirectMatch directives as long as you don't mind adding hundreds of RewriteConds to your existing Rewrite Rule... and keeping those RewriteConds 'in sync' with your RedirectMatches in perpetuity. :(
One other thing, though. Be sure that you are truly using the available power of regular expressions in those current RedirectMatches or in the replacement RewriteRules. You may not actually need hundreds of them if you make good use of pattern-matching and back-references, and if the URLs to be redirected are structurally similar -- i.e. close to each other in code-space. You may be able to sharply reduce the number of directives required, simply by using pattern-matching to better advantage.
Also, since you've got server config access, you could look into using RewriteMap in mod_rewrite if you cannot reduce the number of redirects as described in the preceding paragraph. Although this still requires you to switch the mod_alias RedirectMatch directives over to mod_rewrite, it may provide you with a much-easier-to-maintain solution, as the old->new URL map will be contained in a data table and all of your redirects could require only a single RewriteRule...
Jim
"Be sure that any redirect is set as a 301, that every rule has an L flag, and that all redirects include the protocol and domain name in the target."
then you will have a 301 redirect. Using the R=301 flag will force the redirect.
Test your rules using Live HTTP Headers for Mozilla, or similar. Let's also see a couple of lines of example code.
[edited by: g1smd at 2:58 pm (utc) on Dec. 7, 2009]
Thanks,
Jim
I have changed the redirectmatches as follows:
To handle a request to www.domainname.com/test following rewrite rule is applied...
RewriteRule ^/test/?$ [servername:portnumber...] [P,L,NC].
Is thr any way we can log it as a 301 request?
The rule you posted immediately-preceding is for a proxy through-put, not a redirect. If you really want a redirect, the RewriteRule flags should be "[R=301,L]" or "[NC,R=301,L]".
However, I don't think you're on the right track with that rule anyway. Converting a RedirectMatch to a RewriteRule is fairly simple:
RedirectMatch (?i)^/test.*$ /test/AboutThetest/ RewriteRule ^/test http://www.example.com/test/AboutTheTest/ [NC,R=301,L] Jim