joeinfo
joeinfo

Reputation: 37

How to do a 301 redirect of dynamic pages with query strings to static pages?

I used to have a photo gallery that dynamically generated 1 page per photo, assigning each page's url a unique query string, such as:

https://www.example.com/photos/index.php?start=1 and https://www.example.com/photos/index.php?start=2

Since I don't have a ton of photos in my gallery, I've turned each page into static urls (which might help their seo rankings). Those same pages now have static urls such as:

https://www.example.com/photos/winterstorm2007.php and https://www.example.com/photos/new-chickens.php

Here is what I tried in the htaccess file inside my photos directory -- which just throws a 404 error:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^index\.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]

RewriteCond %{QUERY_STRING} ^index\.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]

Upvotes: 0

Views: 319

Answers (1)

anubhava
anubhava

Reputation: 785186

QUERY_STRING variable only contains query part after ? in URL.

You may use THE_REQUEST to match REQUEST_URI and query:

RewriteCond %{THE_REQUEST} /index\.php\?start=1\s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]

RewriteCond %{THE_REQUEST} /index\.php\?start=2\s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]

Also note trailing ? in target URI to strip off previous query strings.

Upvotes: 1

Related Questions