htaccess重写条件不起作用

时间:2013-09-29 05:33:27

标签: php apache .htaccess mod-rewrite url-rewriting

我正在处理我网站的网址。我刚刚重写了URL来自

http://www.website.com/index.php?url=some-text

http://www.website.com/some-text

为此,我使用以下代码.htaccess

Options +SymLinksIfOwnerMatch
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]


现在,我想在网址中添加另一个变量page number

http://www.website.com/index.php?url=some-text&page=1

我想像

一样重写它
http://www.website.com/some-text/1/
AND
http://www.website.com/some-text/1

我尝试过以下代码,但有些原因它无法正常工作,它显示服务器错误。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*)$ index.php?url=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/(.*)/$ index.php?url=$1&page=$2 [L,QSA]

1 个答案:

答案 0 :(得分:1)

您不需要2条新规则。只有一个可选/就足够了。您的完整代码:

Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine on

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]

新代码中的另一个问题是RewriteCond未应用于上一个RewriteRule,因为这些仅适用于下一个RewriteRule