【发布时间】:2015-01-13 10:26:01
【问题描述】:
我需要一些帮助才能在我的网站上获得友好的 URL。
这些是我需要重写的链接:
www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number
这是我的 .htaccess。我解决了第一个链接的问题,但是我不能解决第二个链接的问题。
我想要第二个链接:
www.example.com/profile/name or
www.example.com/profile/id
这是我的 .htaccess 文件:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]
# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
我上面提交的代码解决了我的问题。
【问题讨论】:
标签: .htaccess mod-rewrite url-rewriting seo friendly-url