本文介绍了带有参数的.html的漂亮URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个仅由 .html
前端组成的网站,我想拥有漂亮的URL.我的目标是创建类似这样的东西
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
和一个特定页面
http://test.com/my-prettlink.html?id=1 => http://test.com/my-prettlink/1
我尝试了这个位于项目根目录中的 .htaccess
,但是只能删除 .html
.
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
推荐答案
更一般的规则应该放在更具体的规则之前.因此,请尝试:
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]
,并在标头中添加< base href ='/subdirectory/'/>
这篇关于带有参数的.html的漂亮URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!