[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-20
5
Medium Priority
?
435 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:14100
  • 3
5 Comments
 
LVL 15

Expert Comment

by:m1tk4
ID: 16500336
RewriteRule ^product/(.*)\.html\??(.*) product.php?productid=$1&$2 [qsappend,redirect,last]
0
 
LVL 15

Expert Comment

by:m1tk4
ID: 16500348
>>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
 

Author Comment

by:14100
ID: 16500408
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
 
LVL 15

Expert Comment

by:m1tk4
ID: 16500554
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
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 16502053
>>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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question