Kevinleary.net
Kevinleary.net

Reputation: 9670

mod_rewrite 301 redirect not working

I'm trying to set up a rewrite that will redirect this URL:

/video-2011.php?video=150

to this:

/video/150/freeform-title-text-here/

I have the rewrite working using this line in my HTACCESS:

RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [L]

But as soon I add R=301 into the mix, it breaks. Any ideas?

Here's the full HTACCESS when it breaks:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [R=301,L]

Any help is greatly appreciated, thanks!

Upvotes: 0

Views: 1238

Answers (2)

Tomgrohl
Tomgrohl

Reputation: 1767

I think you might be missing a line from your .Htaccess.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/([a-z0-9-_]+)$ video-2011.php?video=$1 [L]
#New
RewriteCond %{REQUEST_URI} !^/video/([0-9]+)/([a-z0-9-_]+)/?$
RewriteCond %{QUERY_STRING} ^video=([0-9]+)&name=([a-z0-9-_]+)/?$
RewriteRule ^video-2011.php$ video/%1/%2/? [R=301,L]

I assuming you want to rewrite the url if it is:

/video/150/freeform-title-text-here/

And redirect the url if it is:

/video-2011.php?video=150

to:

/video/150/freeform-title-text-here/

So this way it keeps the urls looking pretty and tidy.

Please correct me if I am wrong.

Edit

I've added in a RewriteCond to stop the second rewrite happening.

As the first rule will obviously rewrite:

/video/150/freeform-title-text-here/

Which means the query string you don't see:

/video-2011.php?video=150

Would of made the second rule happen too.

Upvotes: 2

anubhava
anubhava

Reputation: 785128

Can you try:

RewriteRule ^video/([0-9]+)/ /video-2011.php?video=$1 [R=301,L,NC,QSA]

And yes it will redirect /video/150/foo to /video.php?video=150 not the other way around as you've stated in your question.

Upvotes: 0

Related Questions