htaccess rewrite condition not working

喜欢而已 提交于 2019-12-13 19:27:09

问题


I am working on URL of my website. I have just rewrite the URL From

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

to

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

For this I'm using following code of .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]


Now, I want to add another variable page number in URL.

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

I want to rewrite it like

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

I have tried following code but some reason it is not working, it showing me server error.

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:


You don't need 2 new rules. Just one with optional / will be enough. Your complete code:

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]

Another problem in your new code is that RewriteCond are not getting applied for last RewriteRule since these are applicable to very next RewriteRule only.



来源:https://stackoverflow.com/questions/19074781/htaccess-rewrite-condition-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!