htaccess rewrite condition not working

2019-08-09 03:22发布

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条回答
ら.Afraid
2楼-- · 2019-08-09 03:56

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.

查看更多
登录 后发表回答