Forum Moderators: phranque

Message Too Old, No Replies

Newbie help for dynamic to static pages

dynami static pages redirection

         

khalid

10:19 am on Feb 7, 2012 (gmt 0)

10+ Year Member



This is very confusing to me. I read but could not figure out how to do it properly.

In my site I have categories and products.

Link for categories:
http://mysiteurl/categories.php?catid=1

Link for Products:
http://mysiteurl/products.php?prodid=1


I want the static urls be:
http://mysiteurl/categories-1.html

and
http://mysiteurl/products-1.html


And I need whatever was in the search engine before to take them to the static urls.

Also, if someone tries to access:
http://mysiteurl/categories-.html

or
http://mysiteurl/categories-abc.html

i.e without the category id number or with text in place of the id number, it should thorugh a 404 error page.

Can you please help me with .htaccess rewrite rules?

Thanks,

g1smd

8:02 pm on Feb 7, 2012 (gmt 0)

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



As this is a question that is asked every few days, it's one of those where we insist on seeing your code to debug.

If you haven't got any code, there's several thousand prior threads with code snippets and a lot of explanation already posted and just waiting to be read. :)

khalid

7:27 am on Feb 9, 2012 (gmt 0)

10+ Year Member



g1smd: Thank you for your reply.
I will look for the serveral thousand threads about this subject.

g1smd

9:52 am on Feb 9, 2012 (gmt 0)

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



Pick a dozen threads and have a read through. You'll soon get to the point where you see us repeating the same things over and over again.

We're happy to help you debug your code when you post it.

khalid

4:55 pm on Feb 9, 2012 (gmt 0)

10+ Year Member



OK, based on this thread:
[webmasterworld.com...]

this is my code which seems to be working:


RewriteRule ^categories-([0-9]*)\.html$ categories.php?catid=$1
RewriteRule ^products-([0-9]*)\.html$ products.php?prodid=$1

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /categories\.php\?catid=([^\ ]+)\ HTTP/
RewriteRule ^categories\.php$ http://mysiteURL/categories-%1.html? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.php\?catid=([^\ ]+)\ HTTP/
RewriteRule ^products\.php$ http://mysiteURL/products-%1.html? [R=301,L]


Is my code OK?

Thanks,

g1smd

6:09 pm on Feb 9, 2012 (gmt 0)

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



That's a very good first posting of code. It works for "good" requests, but there's several types of "bad" request where it might fall over.

The first two rules are internal rewrites.

Add the [L] flag to both and move them both to the bottom of the page.

^categories-([0-9]*)\.html$
allows for zero digits. Change the * to a + here and in the other rule.

The other two rules are redirects. They look OK except for one minor suggestion.

/categories\.php\?catid=([^\ ]+)\ /HTTP
will fail if there are additional parameters.
You probably need
/categories\.php\?catid=([^&\ ]+)(&[^&\ ]+)*\ /HTTP
or similar.
This allows other for parameters to follow and for the redirect to still happen.

You should add your standard non-www to www redirect directly after those two redirects and before the two rewrites.

Add a blank line after every
RewriteRule
for clarity.

Add a
# comment
before every block of code. You'll have forgotten what each bit of code is for by next week/month/year.

khalid

6:53 pm on Feb 9, 2012 (gmt 0)

10+ Year Member



Thank you very much for your help.

I modified the code and it is working fine.

g1smd

6:57 pm on Feb 9, 2012 (gmt 0)

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



Excellent!

Make sure to test with both good and bad requests and each of those for both www and for non-www.

Use the Live HTTP Headers extension for Firefox to ensure correct responses and to be sure there's no redirection chain.