【问题标题】:Rewrite URL of custom php based E-Commerce Website重写基于 php 的自定义电子商务网站的 URL
【发布时间】:2013-10-05 16:26:11
【问题描述】:
我已经尝试了很多,但是它们对我不起作用,所以我必须提出这个我需要非常具体的解决方案的查询。
没有使用 CMS。该网站是一个使用php开发的电子商务网站
我的网址结构是
- 对于产品页面 =
https://www[dot]sitename.com/product.php?product=productid
- 对于类别页面 =
https://www[dot]sitename.com/category.php?category=category-name
- 对于子目录页面 =
https://www[dot]sitename.com/category.php?subcategory=sub-category-name
我想要
- 对于产品页面 =
https://www[dot]sitename.com/product/productid/
- 对于类别页面=
https://www[dot]sitename.com/category/category-name
- 对于子目录页面 =
https://www[dot]sitename.com/category/subcategory/sub-category-name
如何使用 .htaccess 来做到这一点?
【问题讨论】:
标签:
php
mysql
.htaccess
url-rewriting
rewrite
【解决方案1】:
将此添加到您的网络根/ 目录中的.htaccess
RewriteEngine on
RewriteBase /
# Products
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^product/([^/]+)/?$ product.php?product=$1 [NC,QSA,L]
# Categories
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^category/([^/]+)/?$ category.php?category=$1 [NC,QSA,L]
# Sub-categories
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^category/subcategory/([^/]+)/?$ category.php?subcategory=$1 [NC,QSA,L]
【解决方案2】:
以下 htaccess 规则对我有用,希望这些对你有用。
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?category=$1 [NC,L] # Handle category
RewriteRule ^category/subcategory/([A-Za-z0-9-]+)/?$ category.php?subcategory=$1 [NC,L] # Handle sub category
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product=$1 [NC,L] # Handle product
【解决方案3】:
感谢大家的建议。然而,没有一种方法对我有用。让我再添加一些关于网站和托管的内容,这可能有助于您分析我的问题并提供替代解决方案。
- 网站托管在 Godaddy 共享主机上
- 该网站未使用任何 CMS