Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 将url重写为不带html结尾的url(.html)_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess 将url重写为不带html结尾的url(.html)

.htaccess 将url重写为不带html结尾的url(.html),.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我想重定向(301)所有不带.html的URL到.html。 例如: example.com/site应重定向到example.com/site.html 我尝试了以下方法: RewriteRule (.+)$ /$1.html [L,R=301] 您的规则可能会导致重定向循环,因为即使在URL已被重定向之后,它也会重写URL 在您的模式中,您需要确保URL不会以.html结尾 您可以使用否定模式来实现这一点: RewriteRule !\.html$ %{REQUEST_URI}.html

我想重定向(301)所有不带.html的URL到.html。 例如:

example.com/site应重定向到example.com/site.html

我尝试了以下方法:

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

您的规则可能会导致重定向循环,因为即使在URL已被重定向之后,它也会重写URL

在您的模式中,您需要确保URL不会以
.html
结尾

您可以使用否定模式来实现这一点:

RewriteRule !\.html$ %{REQUEST_URI}.html [L,R=301]
或者,如果您不想将此应用于空路径(例如在您的示例中),或者需要重用模式的匹配项,则可以使用:

请注意,这是一个非常简单的示例,仅适用于您描述的确切情况。这还会将以
.htm
结尾的URL重写为
.htm.html
,这可能是您想要的,也可能不是您想要的

RewriteCond %{REQUEST_URI} !\.html$
RewriteRule (.+)$ $1.html [L,R=301]