url rewriting using htaccess for category & product in php

佐手、 提交于 2019-12-13 08:45:01

问题


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


回答1:


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]


来源:https://stackoverflow.com/questions/40070981/url-rewriting-using-htaccess-for-category-product-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!