...

View Full Version : Peculiar mod_rewrite question



vkloss
08-03-2011, 01:52 PM
Hi there,

I have a rather strange URL from my client's old website that I need to redirect.

It goes like this. I need to change this:

http://www.mrmoco.com/index2.htm?sidebar.htm&/Products/Rigs/milo.htm

Into this:

http://www.mrmoco.com/Products/Rigs/milo.htm

Basically, I just need to remove this part of the query string: "index2.htm?sidebar.htm&/"

This rule should obviously only happen if such a string exists within the URL.

It's not a customary query string. I don't know if that makes any difference, but it has stopped my simplistic solutions from working.

Any help much appreciated.

-Victor

Cags
08-03-2011, 10:22 PM
Something along these lines should do the job...


RewriteEngine On

RewriteCond %{QUERY_STRING} &/(Products/Rigs/milo.htm)$
RewriteRule .* /%1? [R=302]

...obviously that's pretty specific and will only work for a single url, but to make it more generic you just need to replace the /Products/Rigs/milo.htm part with a Regex pattern. I'm not familiar with the URI design for this project, but I would assume that something like this would be suitable...


RewriteEngine On

RewriteCond %{QUERY_STRING} &/([^&]+\.htm)$
RewriteRule .* /%1? [R=302]

vkloss
08-04-2011, 10:38 AM
That works a charm, thank you very much!

My only (hypothetical) question is, the rewrite condition will only match .htm files, is that correct? How could I get it to match any type of file? (eg. .PHP, .HTML etc.)

-Victor

Cags
08-04-2011, 02:43 PM
Well you could just remove the \.htm part of the pattern, but this could potentially lead to false positives. A safer approach would probably be to include a whitelist like so \.(htm|php|foo)

vkloss
08-04-2011, 04:37 PM
Makes sense, thanks for your help!

-Victor



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum