Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-03-2011, 12:52 PM   PM User | #1
vkloss
New to the CF scene

 
Join Date: Aug 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
vkloss is an unknown quantity at this point
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
vkloss is offline   Reply With Quote
Old 08-03-2011, 09:22 PM   PM User | #2
Cags
New Coder

 
Join Date: Jul 2011
Posts: 67
Thanks: 0
Thanked 13 Times in 13 Posts
Cags is an unknown quantity at this point
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]
Cags is offline   Reply With Quote
Users who have thanked Cags for this post:
vkloss (08-04-2011)
Old 08-04-2011, 09:38 AM   PM User | #3
vkloss
New to the CF scene

 
Join Date: Aug 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
vkloss is an unknown quantity at this point
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
vkloss is offline   Reply With Quote
Old 08-04-2011, 01:43 PM   PM User | #4
Cags
New Coder

 
Join Date: Jul 2011
Posts: 67
Thanks: 0
Thanked 13 Times in 13 Posts
Cags is an unknown quantity at this point
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)
Cags is offline   Reply With Quote
Old 08-04-2011, 03:37 PM   PM User | #5
vkloss
New to the CF scene

 
Join Date: Aug 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
vkloss is an unknown quantity at this point
Makes sense, thanks for your help!

-Victor
vkloss is offline   Reply With Quote
Reply

Bookmarks

Tags
mod_rewrite

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:12 PM.


Advertisement
Log in to turn off these ads.