CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Apache configuration (http://www.codingforums.com/forumdisplay.php?f=69)
-   -   Peculiar mod_rewrite question (http://www.codingforums.com/showthread.php?t=233922)

vkloss 08-03-2011 12:52 PM

Peculiar mod_rewrite question
 
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?sid.../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 09:22 PM

Something along these lines should do the job...

Code:

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...

Code:

RewriteEngine On

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


vkloss 08-04-2011 09: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 01: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 03:37 PM

Makes sense, thanks for your help!

-Victor


All times are GMT +1. The time now is 08:20 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.