我正在使用.htaccess以获得更好的URL。
我有这个php页面:
treatments-line.php
和
#rewrite treatments-line
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]
我可以成功进入页面/ treatments / line / the_id_of_the_page。
但是,当我创建页面treatments.php时,对treatments-line的重写无效,它只是显示了首页。
为了进行测试,当我将重写方法更改为此:
#rewrite treatments-line
RewriteRule ^/?treatment/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]
我刚刚将名称更改为处理(不带“ s”),在/ treatment / line / the_id_of_the_page上的重写工作正常,但我不想更改名称。
因此,我可以看到该名称存在冲突,这是由于treatments.php文件所致。
我该如何解决?
这两个PHP页面位于网站的根目录中。
编辑2:
这是我的整个.htaccess代码:
Options -MultiViews
RewriteEngine on
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_URL} /(.*)$
RewriteRule ^(.*)$ %1.php [NC,L]
#redirect posts-view
RewriteRule ^/?post/([0-9]+)$ posts-view.php?id=$1 [NC,L]
#rewrite products-zone
RewriteRule ^/?products/zone/([0-9]+)$ products-zone.php?id=$1 [NC,L]
#rewrite products-category
RewriteRule ^/?products/category/([0-9]+)$ products-category.php?id=$1 [NC,L]
#rewrite products-solution
RewriteRule ^/?products/solution/([0-9]+)$ products-solution.php?id=$1 [NC,L]
#rewrite products-line
RewriteRule ^/?products/line/([0-9]+)$ products-line.php?id=$1 [NC,L]
#rewrite treatments-line
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]
#rewrite treatments-category
RewriteRule ^/?treatments/category/([0-9]+)$ treatments-category.php?id=$1 [NC,L]
#rewrite product-view
RewriteRule ^/?products/([0-9]+)$ product-view.php?id=$1 [NC,L]
#redirect lang
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L]
#RewriteRule ^([^\.]+)/?$ index.php?lang=$1 [L]
Options -MultiViews
RewriteEngine On
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]
添加Options -MultiViews
可以解决您的问题。
有关更多信息Options -MultiViews
:
MultiViews搜索通过“ MultiViews选项”启用。如果服务器收到对/ some / dir / foo的请求,而/ some / dir / foo不存在,则服务器将读取目录以查找所有名为foo。*的文件,并有效地伪造一个类型映射,将所有这些命名为文件,为其分配相同的媒体类型和内容编码(如果客户端按名称要求提供其中一种)。然后,它选择最符合客户要求的匹配项,并返回该文档。
http://httpd.apache.org/docs/2.2/en/mod/mod_negotiation.html#multiviews
根据您的.htaccess代码以及注释,您需要将#remove .php
规则的位置更改为.htaccess的末尾。因此,这就是您的.htaccess代码应如下所示的方式:
Options -MultiViews
RewriteEngine on
#redirect posts-view
RewriteRule ^/?post/([0-9]+)$ posts-view.php?id=$1 [NC,L]
#rewrite products-zone
RewriteRule ^/?products/zone/([0-9]+)$ products-zone.php?id=$1 [NC,L]
#rewrite products-category
RewriteRule ^/?products/category/([0-9]+)$ products-category.php?id=$1 [NC,L]
#rewrite products-solution
RewriteRule ^/?products/solution/([0-9]+)$ products-solution.php?id=$1 [NC,L]
#rewrite products-line
RewriteRule ^/?products/line/([0-9]+)$ products-line.php?id=$1 [NC,L]
#rewrite treatments-line
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]
#rewrite treatments-category
RewriteRule ^/?treatments/category/([0-9]+)$ treatments-category.php?id=$1 [NC,L]
#rewrite product-view
RewriteRule ^/?products/([0-9]+)$ product-view.php?id=$1 [NC,L]
#redirect lang
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L]
#RewriteRule ^([^\.]+)/?$ index.php?lang=$1 [L]
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句