使用.htaccess重写的问题

时间:2016-12-18 22:17:14

标签: php .htaccess

我使用.htaccess获取更好的网址。

我有这个php页面:

治疗-line.php

#rewrite treatments-line
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]

我可以成功进入页面/ treatment / line / the_id_of_the_page。

但是当我创建一个页面treatment.php时,处理线的重写不起作用,它只显示了首页。

对于将重写方法更改为此内容的测试:

#rewrite treatments-line
RewriteRule ^/?treatment/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]

我刚刚将名称更改为治疗(没有" s"),/ treatment / line / the_id_of_the_page上的重写工作正常,但我不想更改名称

所以,我可以看到,由于的是treatment.php文件,因此与名称存在冲突。

我该如何解决?

这两个PHP页面位于网站的根目录中。

EDIT2:

这是我的整个.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]

1 个答案:

答案 0 :(得分:2)

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]