Kevlar
Kevlar

Reputation: 334

Re-writing Dynamic URL with HTACCESS

I've been reading a few tutorials online and searched a couple of other questions here but I cant seem to grasp the re-writing of dynamic URLS using the HTACCESS file.

I have URLs like:

products.php?cat=2
products.php?cat=3
products.php?cat=4

I would like them to just say products/2 for example

equipment.php?cat=2&subCat=1
equipment.php?cat=2&subCat=2
equipment.php?cat=2&subCat=3

I would like these to say equipment/2/1 for example

product.php?id=3010-Z89CH24

I would like these to say product/3010-Z89CH24 for example

but so far just trying to change the products.php pages I have this:

    RewriteEngine On
RewriteCond %{HTTP_HOST} ^*******\.com [NC]
RewriteRule (.*) http://www.*******.com/$1 [L,R=301]

Options +FollowSymLinks

RewriteRule ^products/'^([1-9][0-9]{0,2})'$ http://www.*******.com/products.php?cat=/$2 [L] 
# REMOVE PHP EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

the thing is I dont think the end part "/$2" is correct and im unsure of the rest of it too :/

Either way its not working for me and I am stuck and struggling to understand it :(

can someone help me please?

Upvotes: 1

Views: 4731

Answers (3)

anubhava
anubhava

Reputation: 784958

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)&subcat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /$1.php?cat=$2&subcat=$3 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?cat=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteRule ^([^/.]+)/([^/]+)/?$ /$1.php?cat=$2 [L,QSA]

Upvotes: 1

nickgrim
nickgrim

Reputation: 5437

Your problem is that your regexp is wrong - I'd recommend checking out some kind of basic regular-expression tutorial, paying attention to mentions of capture groups.

Without checking - and assuming that your digit-based-restrictions are correct - I'd guess you want a rule something like this:

RewriteRule  ^/products/([1-9][0-9]{0,2})$  /products.php?cat=$1 [L]

If you need to preserve existing query-strings you'll probably want the QSA flag too.

Upvotes: 0

Related Questions