Htaccess - 用正斜杠替换.html

时间:2013-03-29 15:12:22

标签: .htaccess mod-rewrite redirect

我想重定向此表单的网址: /page.html?variable=value&othervar=true&thirdvar=100

对此: /page/?variable=value&othervar=true&thirdvar=100

所以基本上我只想用.html替换URL中间的forward slash,但我需要保留随附的get字符串。这就是我试过的:

  

RewriteRule ^ page.html(。+)$ / page / $ 1 [L,R = 301]

但这似乎对我不起作用。我最近做了类似的事情,但我无法弄清楚我在这里缺少什么。感谢您的任何意见。

1 个答案:

答案 0 :(得分:8)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]

# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]