Anna Riekic
Anna Riekic

Reputation: 183

htaccess 301 redirect after changing file name

I use this rewrite:

RewriteRule post-(.*)\.html$ post.php?id=$1

but have since changed the file name post.php to photo.php so i changed it to

RewriteRule photo-(.*)\.html$ photo.php?id=$1

which is fine but I have been getting errors in my logs when visitors come from search engines and get directed to original file name post.php?id=? so need to add (I think) a 301 redirect, I have tried:

RewriteRule photo-(.*)\.html$ post.php?id=$1

and:

RewriteRule photo-(.*)\.html$ post-(.*)\.html$[L,R=301]

but neither work and to be honest haven't really got a clue, please help.

Upvotes: 1

Views: 509

Answers (1)

Jon Lin
Jon Lin

Reputation: 143966

I think you want it to be the other way around. When people go to your old URL (post.php?id=), redirect to the new one. You either want one of these:

RewriteRule post-(.*)\.html$ photo-$1.html [R=301]

RewriteRule post\.php$ photo.php [R=301]

The first takes a URL like http://hostname.com/post-1234.html and redirects the browser to http://hostname.com/photo-1234.html so your rules can rewrite it to photo.php, the second does the same except http://hostname.com/post.php?id=1234 to http://hostname.com/photo.php?id=1234 (note that the query string automatically gets appended to the end in the second rule).

Upvotes: 1

Related Questions