views:

397

answers:

1

Hello! I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.

Say I want to forward this URL: /cansas.php?m=2&id=2-0-0-0&sid=cansas to /cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].

I have this in my .htaccess:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]

I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.

How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:

/cansas.php?m=2&id=2-0-0-0&sid=cansas

/cansas.php?m=2&id=2-1-0-0&sid=cansas

/cansas.php?m=2&id=2-2-0-0&sid=cansas

Any help would be greatly appreciated :)

Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)

+1  A: 

You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:

RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

If you want to check for just one parameter, use this:

RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

And to do this just for initial requests, you need to check the request line:

RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
Gumbo
Thanks alot, Gumbo. That solved it perfectly.
Mighty Mac