友情网址与mod_rewrite

时间:2013-08-08 11:47:26

标签: php mod-rewrite url-rewriting friendly-url

我有这样的网址

http://localhost/rilo/listing.php?id=2

其中“rilo”是根,我想获得像

这样的友好网址
http_//localhost/rilo/listing/2.html

这是htaccess

Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rilo

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?listing/([a-zA-Z_]+).html$ listing.php?id=$1 [QSA,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
你能帮帮我吗?错误在哪里?

1 个答案:

答案 0 :(得分:1)

用以下代码替换您的代码:

Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rilo

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?listing/([^.]+)\.html$ listing.php?id=$1 [QSA,L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[^/]+/(listing)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ %1/%2.html? [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]