Forum Moderators: phranque

Message Too Old, No Replies

htaccess issue

         

pinkstor

5:49 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi, I'm trying to use RedirectMatch to change the urls on my site. The current urls on my site are like this:

[testsite.com...]

I did a RewriteMatch to change it to this:

[testsite.com...]

Easy as pie, right? So here's my RedirectMatch:

RedirectMatch 301 /deals/([a-zA-Z0-9¦\-]+)_([0-9]+)\.html [testsite.com...]

It works (kind of), but I end up with this:

[testsite.com...]

Obviously, I don't want all that query string data at the end. What am I doing wrong? Thanks!

jdMorgan

3:22 am on Mar 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may not be doing anything wrong, but it's obvious that you have some mod_rewrite code on that server to map 'static' search-engine-friendly URLs to one or more scripts which actually generate the HTML pages.

So what's happening is this:

You request the static URL.
The mod_rewrite code rewrites that URL into the query-string-form needed to call your script.
Now your RedirectMatch code runs, modifies only the URL (and not the appended query string), and invokes an external redirect.
This causes the query-stringed URL to appear in your browser address bar as your browser now asks for it in response to the redirect response from the server.

Possible solution (assuming you can put all the code into the same .htaccess file): Use mod_rewrite directives instead of mod_alias directives like RedirectMatch. Put your new redirect ahead of the rules that internally rewrite the script requests.

By having all the code in the same file, and by using the same module to do both functions, you can enforce a desired execution order; Otherwise, this can be difficult or even impossible to do.

In case it's not clear, each Apache module reads your access file in turn. The order in which they do this is set by the server configuration. As each module processes your .htaccess file, it executes only the directives that it understands, and ignores everything else. Therefore, the code is not executed in the order that you wrote it; Only the directives for any one given module are executed in the order in which they appear in the file. So, maybe mod_rewrite executes first, followed by mod_alias, which is your current case. But it could be the other way around -- it is mod_alias first, mod_rewrite second on the server I was working on today.

Best practice: Don't mix modules except when absolutely necessary.

Jim

pinkstor

2:34 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



You're right, that's what was happening - I was basically rewriting my rewrite. I took out the old rewrite, but left in the RedirectMatch, and it works like a charm. (My reasons for leaving in the old rewrite rule, while clear to me at the time, were wrong. :))

Thanks for your help!