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

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

Apache Web Server Forum

    
redirectmatches not working because of conflict with new rewrite rule
job_ppa




msg:4036382
 6:18 pm on Dec 3, 2009 (gmt 0)

We got a website up and running and have to add rewrite rule

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?

 

g1smd




msg:4036391
 6:23 pm on Dec 3, 2009 (gmt 0)

If this rule is placed in your .htaccess file the RewriteRule will not 'see' the leading slash and can never match any incoming URL request. That's a common problem seen round here.

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.

job_ppa




msg:4036398
 6:33 pm on Dec 3, 2009 (gmt 0)

Hi

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]

jdMorgan




msg:4036405
 6:40 pm on Dec 3, 2009 (gmt 0)

The simplest answer -- as already given above, is: Don't mix mod_rewrite and mod_alias directives. Use a RewriteRule to externally redirect or internally rewrite /test to /test/AboutTheTest/

You can then control rule execution order and precedence by the proper ordering of your rules and the use of the [L] flag.

Jim

job_ppa




msg:4036411
 6:47 pm on Dec 3, 2009 (gmt 0)

Thanks for your quick response.

But there are hundreds of redirectmatches. how to rewrite internally?

It's not possible to internally rewrite for each of the redirectmatches is it?

sorry if im being stupid understand im a newbie....

g1smd




msg:4036417
 6:52 pm on Dec 3, 2009 (gmt 0)

Change every RedirectMatch rule to be a RewriteRule with [R=301,L] flags attatched to each one.

The Rule target should include protocol and domain name too, not just a URL path.

job_ppa




msg:4036443
 7:32 pm on Dec 3, 2009 (gmt 0)

mate i might be just naive but is their not a way where i can just specify the kinda precedence where redirectmatches are executed first instead of rewrite rules..?

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

g1smd




msg:4036446
 7:35 pm on Dec 3, 2009 (gmt 0)

What happens is that the server evaluates all the RedirectMatches first, and then evaluates all of the RewriteRules; or maybe it does it the other way round. There's no way you can control this, so all of the rules need to use the same module.

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.

jdMorgan




msg:4036535
 10:42 pm on Dec 3, 2009 (gmt 0)

Yes sorry, but the answer isn't 'easy', and it's not the one you hoped for, but it's correct.

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

job_ppa




msg:4038739
 2:30 pm on Dec 7, 2009 (gmt 0)

Thankyou very much for your help dude. I have converted all redirectmatches to rewrite rule.

But is their any way to log it as a 301 redirect.

g1smd




msg:4038753
 2:56 pm on Dec 7, 2009 (gmt 0)

If you are doing exactly what I said several posts back...

"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]

jdMorgan




msg:4038755
 2:57 pm on Dec 7, 2009 (gmt 0)

If your RewriteRule syntax and server configuration are correct, these *should already* be getting logged as 301 redirects. Please be specific, describe what you see in your log file and your browser behavior, and post an example of one of your new rules, changing (only) the domain to "example.com"

Thanks,
Jim

job_ppa




msg:4038768
 3:20 pm on Dec 7, 2009 (gmt 0)

at the moment it is logging 304 status code in the logs.

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?

jdMorgan




msg:4038799
 3:52 pm on Dec 7, 2009 (gmt 0)

You need to completely-flush your browser cache before testing any new server-side code.

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/
becomes
RewriteRule ^/test http://www.example.com/test/AboutTheTest/ [NC,R=301,L]

Jim

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