带参数的.html漂亮网址

时间:2015-11-16 17:36:49

标签: .htaccess mod-rewrite url-rewriting friendly-url

我的网站只包含.html前端,我希望拥有漂亮的网址。我的目标是创造这样的东西

http://test.com/mypage.html  => http://test.com/mypage
http://test.com/mypage1.html => http://test.com/mypage1

和一个特定页面

http://test.com/my-prettlink.html?id=1 => http://test.com/my-prettlink/1

我尝试了这个位于项目根目录中的.htaccess,但只删除了.html

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^my-prettylink/(\d+)*$ ./my-prettylink.html?id=$1

1 个答案:

答案 0 :(得分:1)

更具体的规则应该出现在一般规则之前。所以,试试:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdirectory/

RewriteCond %{THE_REQUEST} ^GET\ /(subdirectory/my-prettylink)\.html\?id=(\d+) [NC]
RewriteRule ^my-prettylink\.html$ /%1/%2? [R=301,L]

RewriteCond %{THE_REQUEST} ^GET\ /(subdirectory/.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^my-prettylink/(\d+)$ my-prettylink.html?id=$1 [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(?!js|css) %{REQUEST_URI}.html [L]

并在标题

中添加<base href='/subdirectory/' />