Pretty URL for .html with parameters

北战南征 提交于 2021-02-05 07:10:29

问题


I have a website which consists only from .html frontend and I want to have pretty URLs. My goal is to create something like this

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

and for one specific page

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

I tried this .htaccess which is located in project root but only removing .html works.

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:


More specific rules should come before general rules. So, try:

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]

and also add <base href='/subdirectory/' /> in header



来源:https://stackoverflow.com/questions/33741348/pretty-url-for-html-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!