SEO implementation, using htaccess (rewrite), php, regex?

I'm working with a script, where regex will be used to turn all urls of a certain format, into SEO-friendly urls.
i.e.-
product.php?productid=2930
becomes
product/2930.html   (though this will eventually show the actual product name, but that's later...)

I need htaccess rules that could match:
product/2930.html
and map it to the original url.

Also, an attempt to reach:
product.php?productid=2930
should redirect to the new SEO url.


There's one more catch, if there are additional variables in the url, I need a suggestion on how to capture those additional variables and pass them to the SEO url (this is optional, not required to win the points). i.e. - product.php?productid=2930&cat=252
14100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

m1tk4Commented:
RewriteRule ^product/(.*)\.html\??(.*) product.php?productid=$1&$2 [qsappend,redirect,last]
0
m1tk4Commented:
>>Also, an attempt to reach:
>>product.php?productid=2930 -

you can't do that because it'll create an endless loop -

pruduct.php?productid=2930 redirects to product/2930.html, which redirects to product.php?productid=2930
0
14100Author Commented:
Hmm... then is there no way that you know of, to ensure that the html url is the only one used? I need to maintain backwards compatibility with urls that already have propagated on the web, so if someone the old url structure, they need to be shown the new url.
0
m1tk4Commented:
the backwards compatibility is there, since all your old urls still work.

What you could do is analyze _SERVER['REQUEST_URI'] in your php and send a redirect header() there, it's just you can't really do both redirects in mod_rewrite.
0
caterham_wwwCommented:
>>an attempt to reach:
product.php?productid=2930
should redirect to the new SEO url.<<

RewriteCond %{THE_REQUEST} ^[A-Z]{3,4}\ /product.php\?productid=([0-9]+)\ HTTP
RewriteRule ^product\.php$ /product/%1.html? [R=301,L]

>>product.php?productid=2930
becomes
product/2930.html<<

RewriteRule ^product/([0-9]+)\.html$ /product.php?productid=$1 [QSA,L]


> if there are additional variables in the url, I need a suggestion on how to capture those additional variables and pass them to the SEO url

use e.g. /product/id-cat.html, i.e.  /product/2930-252.html


RewriteCond %{THE_REQUEST} ^[A-Z]{3,4}\ /product.php\?productid=([0-9]+)&cat=([0-9]+)\ HTTP
RewriteRule ^product\.php$ /product/%1-%2.html? [R=301,L]

RewriteRule ^product/([0-9]+)-([0-9]+)\.html$ /product.php?productid=$1&cat=$2 [QSA,L]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.