htaccess mod_rewrite用于基于动态id的不同url

时间:2015-11-17 11:57:54

标签: php apache .htaccess mod-rewrite

大家好我在网址定制方面有问题我有两种类型的网址

首先是

http://domain/index.php?slug=pagename

我希望它为http://domain/pagename

第二是

http://domain/index.php?slug=pagename&id=id 我想要它 http://domain/pagename/id

我的htaccess是

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.+)$

RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^?]*)$ index.php?slug=$1 

IndexIgnore *

1 个答案:

答案 0 :(得分:0)

试试这个:

RewriteEngine on

RewriteCond %{THE_REQUEST} /index\.php\?slug=([^&]+)&id=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,L,R]

RewriteCond %{THE_REQUEST} /index\.php\?slug=([^\s&]+) [NC]
RewriteRule ^ %1? [NC,L,R
RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^/]+)/?$ index.php?slug=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^/]+)/([^/]+)/?$ index.php?slug=$1&id=$2 [QSA,NC,L]