漂亮的 URL for .html with parameters

Pretty URL for .html with parameters

我有一个仅由 .html 前端组成的网站,我想要漂亮的 URL。我的目标是创建这样的东西

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=

更具体的规则应该先于一般规则。所以,试试:

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= [L]

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

并在 header

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