带有参数的 .html 的漂亮 URL [英] Pretty URL for .html with parameters
本文介绍了带有参数的 .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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文