Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

apache - 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...