.htaccess 带有参数的.html的漂亮URL

.htaccess 带有参数的.html的漂亮URL,.htaccess,mod-rewrite,url-rewriting,friendly-url,.htaccess,Mod Rewrite,Url Rewriting,Friendly Url,我有一个网站,它只包含.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 我尝试了

我有一个网站,它只包含
.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=$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]

并将
添加到标题中

最后的重写路径将永远不会到达。它必须是第一位的,因为它更专业。更一般的规则更接近尾声。当我点击
myprettylink?id=1
时,链接不会改变(但页面可以正常工作),而且
myprettylink/1
会抛出
找不到
。当我删除
/my prettylink.html?id=$1[L]
前面的斜杠时found@JozefDúc文件在哪里?你把重写规则放在哪里?您希望丑陋的URL也重定向到漂亮的URL吗?文件与
.htaccess
文件位于同一目录中。我只想将一个带有查询字符串的页面重定向到pretty URL(正如我在问题中所写的那样),并从所有URL中删除
.html
extension@JozefDúc所有文件都在服务器根目录中吗?或者在某个子目录中。我将相应地更新回复。整个页面位于子目录中