gautam nagraj
gautam nagraj

Reputation: 21

url rewriting using htaccess for category & product in php

please help me to complete my project its about ecommerce site in core php as i am a beginner

for my site i want seo friendly url for category, subcategory, product and product detail page....

i have pages like this :

mysite.com/category.php?category_slug=mobiles         : category page
mysite.com/subcategory.php?subcategory_slug=samsung   :   subcategory page
mysite.com/product.php?product_slug=galaxy-note       :   product page

and i want to rewrite these url like this :

mysite.com/mobiles        :   category
mysite.com/samsung        :   sub category
mysite.com/galaxy-note    :   product page

my htaccess code is


RewriteEngine On
Options -Multiviews
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9-]+)$ category.php?category_slug=$1 [NC,L]

RewriteRule ^([a-zA-Z0-9-]+)$ subcategory.php?subcategory_slug=$1 [NC,L]

RewriteRule ^([a-zA-Z0-9-]+)$ products.php?product_slug=$1 [L]


any help would be appreciated...

Upvotes: 0

Views: 3692

Answers (2)

Muhammad Jaffar
Muhammad Jaffar

Reputation: 1

category/([a-zA-Z0-9-]+)$ category.php?category_slug=$1 [NC,L]

remove the $ sign after category/([a-zA-Z0-9-]+)

Upvotes: 0

Samir Nabil
Samir Nabil

Reputation: 397

add page name before ^

RewriteRule ^category/([a-zA-Z0-9-]+)$ category.php?category_slug=$1 [NC,L]

RewriteRule ^subcategory/([a-zA-Z0-9-]+)$ subcategory.php?subcategory_slug=$1 [NC,L]

RewriteRule ^products/([a-zA-Z0-9-]+)$ products.php?product_slug=$1 [L]

and write your url for category page as example category/mobiles/

UPDATE

The original URL: mysite.com/category.php?category_slug=mobiles The rewritten URL: mysite.com/mobiles

RewriteRule ^([^/]*)$ /category.php?category_slug=$1 [L]

The original URL: mysite.com/subcategory.php?category_slug=mobiles&subcategory_slug=samsung The rewritten URL: mysite.com/mobiles/samsung

RewriteRule ^([^/]*)/([^/]*)$ /subcategory.php?category_slug=$1&subcategory_slug=$2 [L]

The original URL: mysite.com/products.php?category_slug=mobiles&subcategory_slug=samsung&product_slug=galaxy-note The rewritten URL: mysite.com/mobiles/samsung/galaxy-note

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /products.php?category_slug=$1&subcategory_slug=$2&product_slug=$3 [L]

Upvotes: 0

Related Questions