htaccess重写添加.html如果存在,否则保留URL

时间:2013-12-11 04:24:56

标签: regex apache .htaccess mod-rewrite

我有一种情况,我需要在文件存在时进行URL重写,否则保留URL原样。

以下是一个例子:

http://mydomain.com/section -> if http://mydomain.com/section exists go to -> http://mydomain.com/section.html
http://mydomain.com/section -> if http://mydomain.com/section.html DOES NOT exist go to -> http://mydomain.com/section

如何使用重写?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在DOCUMENT_ROOT/.htaccess文件中尝试此规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ /$1.html [L,R=302]