views:

1826

answers:

4

I have a few messy old URLs like...

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1

http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2

...that I want to redirect to the newer, cleaner form...

http://www.example.com/page.php/welcome

http://www.example.com/page.php/prices

I understand I can redirect one page to another with a simple redirect i.e.

Redirect 301 /bunch.of/unneeded/crap http://www.example.com/page.php

But the source page doesn't change, only it's GET vars. I can't figure out how to base the redirect on the value of these GET variables. Can anybody help pls!? I'm fairly handy with the old regexes so I can have a pop at using mod-rewrite if I have to but I'm not clear on the syntax for rewriting GET vars and I'd prefer to avoid the performance hit and use the cleaner Redirect directive. Is there a way? and if not can anyone clue me in as to the right mod-rewrite syntax pls?

Cheers,

Roger.

A: 

In summary, you could use RedirectMatch with a regex that will match the full URL, including query string. That will let you rearrange parts of the URL, but if you have to do conversions like "opendocument&part=1" to "welcome" (where the new URL is completely different from the original one), you might need a RewriteMap - or perhaps better, just send all URLs to page.php and parse the query string in PHP.

EDIT: If it's just a few URLs you want to redirect, you could probably write out individual rules like

RedirectPermanent http://www.example.com/bunch.of/unneeded/crap?opendocument&part=1 http://www.example.com/page.php/welcome
RedirectPermanent http://www.example.com/bunch.of/unneeded/crap?opendocument&part=2 http://www.example.com/page.php/prices
David Zaslavsky
`Redirect` does only work with URL paths and not with the URL query.
Gumbo
+3  A: 

As the parameters in the URL query may have an arbitrary order, you need to use a either one RewriteCond directive for every parameter to check or for every possible permutiation.

Here’s an example with a RewriteCond directive for each parameter:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=1(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/welcome? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=2(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/prices? [L,R=301]

But as you can see, this may get a mess.

So a better approach might be to use a RewriteMap. The easiest would be a plain text file with key and value pairs:

1 welcome
2 prices

To define your map, write the following directive in your server or virual host configuration (this directive is not allowed in per-directory context):

RewriteMap examplemap txt:/path/to/file/map.txt

Then you would just need one rule:

RewriteCond %{QUERY_STRING} ^([^&]&)*opendocument(&|$)
RewriteCond %{QUERY_STRING} ^([^&]&)*part=([0-9]+)(&|$)
RewriteRule ^bunch\.of/unneeded/crap$ /page.php/%{examplemap:%2}? [L,R=301]
Gumbo
Yikes, that's quite a faff init!? You've explained it very well though, thanks :)
That isn’t even everything I could tell you. You could also use a PHP script to solve this problem. And id might even be better as PHP is a mightier language than mod_rewrite. But you’ve asked for a mod_rewrite solution.
Gumbo
A: 

Could you help me with the following:

I want to redirect a page that I had about banks (http://www.domain.com/index.php?option=com_content&task=view&id=70&Itemid=82) that no longer exists to the http://www.domain.com/business/banks/ address using a 301 php redirect.

I'm familiar with a oldpage.html to a newpage.html, but ?option....

Thanks in advance!!!

Jack Ross
A: 

@Jack Ross

RewriteCond %{QUERY_STRING} option=com_content&task=view&id=70&Itemid=82
RewriteRule ^index.php http://www.domain.com/business/banks/? [R=301,L]

The ? will prevent the url the user is sent to from having the same query string as the origin page.

bradym