【发布时间】:2011-10-05 16:18:16
【问题描述】:
我正在尝试使用 php 制作干净的 url,但我遇到了一些错误。希望有人能帮忙。
我的.htaccess如下:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# this is the initialization
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# these are the rewrite conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# and finally, the rewrite rules
RewriteRule ^([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
实际上我正在尝试在子文件夹price/ 中创建干净的网址。
当我输入时:mydomain.com/price/param1 它运行良好,实际上将我重定向到
mydomain.com/price/index.php?var1=param1
但是当我尝试使用更多变量继续前进时,我遇到了问题。当我尝试访问 mydomain.com/price/param1/param2 时,我没有被重定向并且出现 404 错误。
有什么想法吗?提前致谢。
【问题讨论】:
-
此 .htaccess 位于何处 - 在网站根文件夹中还是 /price/.htaccess ?
-
这两个
RewriteCond仅适用于紧随其后的单个规则。 -
.htacces 位于根文件夹中,但这不是问题。 mootinator,这是什么意思?
-
@Dan Stern:这意味着您拥有上面的 .htaccess 代码的方式,如果您访问
/price/var1/var2并且有一些目录与您的 url 的层次结构相同,那么重写就会开始,而且您将无法访问目录。这就是条件存在的原因,!-f和!-d的意思是,仅当请求所在的位置不存在文件或目录时才重写。
标签: php .htaccess http-status-code-404 clean-urls