Forum Moderators: phranque

Message Too Old, No Replies

301 redirecting dynamic urls with query strings to static page

help please: using htaccess to redirect query string pages to static pages

         

ashevilleweb

5:57 pm on Jan 3, 2019 (gmt 0)

5+ Year Member



i was so hopeful that the post at [webmasterworld.com...] would be my answer. But it's not. All i get are 404 errors.

i used to have a photo gallery script 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

any help would be greatly appreciated!
(the more specific and complete, the better since i find bits and pieces of regular expressions extremely difficult to cut and paste)



[edited by: not2easy at 6:33 pm (utc) on Jan 3, 2019]
[edit reason] example.com = readability [/edit]

phranque

11:23 pm on Jan 3, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], ashevilleweb!

what have you tried so far?

ashevilleweb

11:34 pm on Jan 3, 2019 (gmt 0)

5+ Year Member



thank you for replying, phranque.
we just now got it working so i will have to mark this help request as resolved.

here was what worked:

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

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

the secret turned out to be the '\s' at the end of each condition line.

without this, start=1 and start=10 and start=125 were all resolving to the redirect page for start=1.

by adding the '\s' at the end of each condition line, that forced the full query string to get read... rather than the redirect reading only the first digit after start=

i hope this makes sense and helps others.

thank you!

phranque

12:07 am on Jan 4, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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

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


why are you using a 302?
in most cases this should be a 301 unless you have a good reason otherwise.

i would suggest a few other changes:
- if you specify the path part of the url in the Pattern for the RewriteRule, you will avoid firing these rules on every request and therefore avoid evaulating the attached RewriteCond directives unless necessary.
- you really want to use an end end anchor in the pattern for the RewriteCond instead of the \s metacharacter and you can test this against the query string instead of the entire request
- you should specify the full canonical protocol and hostname in the substitution string of the RewriteRule.

perhaps you want this instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^start=1$ [NC]
RewriteRule ^index\.php$ https://www.example.com/photos/winterstorm.php? [R=301,L]

RewriteCond %{QUERY_STRING} ^start=2$ [NC]
RewriteRule ^index\.php$ https://www.example.com/photos/farm-cats.php? [R=301,L]

lucy24

12:22 am on Jan 4, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the secret turned out to be the '\s' at the end of each condition line

Matter of fact, you don't need \s which means “space”, you need \b which means “word boundary”. The \s only works because you’re looking at THE_REQUEST, which has more content after the query string. If instead you looked at QUERY_STRING as phranque suggests, \s will no longer work.

I suggest \b rather than $ because then it will still work if some other parameter sneaks into the mix, as for example if a search engine takes it into its head that you have additional parameters. (Stranger things have happened.) You can also put a \b before "start" for the same reason, though here it really wouldn't matter unless you also have a parameter called, say, "restart". Yes, OK, the ^ might also save a few picoseconds, since the server only has to look at the very beginning of the query string--but this only applies if the query could potentially start with something other than, er, ahem, "start".

ashevilleweb

7:59 pm on Jan 4, 2019 (gmt 0)

5+ Year Member



thank you both for your suggestions!
phranque: i mistakenly left the 302 redirect in my example code above. because i was making so many attempts and changing my htaccess constantly, i was afraid setting those redirects as 301 may cause cache headaches.
before uploading the final file, however, i did have those changed to 301.
if you can address that... would starting off with 301 have created potential cache problems during testing? (i test on a live server since i don't have a local server set up on my machine.)

so, i replaced the code i had with phranque's and... yes! it works perfectly. i then used lucky24's suggestion of replacing phranque's ending $ with \s and it still works perfectly. so, that is what i will go with.
thank you both very much!

lucy24

8:18 pm on Jan 4, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



would starting off with 301 have created potential cache problems during testing?
Browsers do remember 301 responses, so that's potentially an extra step in testing. You have to hit the Refresh/Reload button. Using 302 instead of 301 in testing is a pretty good idea (which I, personally, always forget); just remember to change it to 301 once you're sure it is working right.

replacing phranque's ending $ with \s
I hope that was a typo for \b

ashevilleweb

5:11 pm on Jan 5, 2019 (gmt 0)

5+ Year Member



ugh! yes.... you have a wonderfully sharp eye that you caught that. \b is what i went with. thanks!