首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >永久重定向删除URL中的文件扩展名

永久重定向删除URL中的文件扩展名
EN

Stack Overflow用户
提问于 2018-08-05 06:30:49
回答 1查看 70关注 0票数 -1

当使用以.html结尾的URL访问页面时,我希望该URL更改为无扩展名,并且报告301永久重定向。我遇到了严重的困难。在阅读了大量的文档和教程,并搜索了几个小时的Stack Overflow之后,我得到的最接近的结果是相反的(没有添加扩展名的URL),代码如下:

代码语言:javascript
复制
<Location />
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{REQUEST_URI} !\.html$
    RewriteRule ^(.*)$ https://%{HTTP:Host}%{REQUEST_URI}.html [L,R=permanent]
</Location>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-05 12:12:04

这可以通过两个步骤完成,使用REDIRECT_LOOP环境变量来防止循环,使用web根目录部分中的指令(或web根目录中的.htaccess ),以便RewriteRule中匹配的字符串可用于永久重定向。

代码语言:javascript
复制
<Directory "/var/www/html">

 RewriteEngine On

 RewriteCond %{REQUEST_FILENAME} !-d # if the request is not a directory
 RewriteCond %{REQUEST_FILENAME} !-f # if the request is not a file
 RewriteCond %{REQUEST_FILENAME}\.html -f # if adding .html it is a file
 RewriteCond %{REQUEST_URI} !\.html$ # if the request doesn't end in .html
 RewriteCond %{REQUEST_URI} !/$ # if the request doesn't end in /
 RewriteRule ^(.*)$ $1.html [L,E=LOOP:1] # then return the request as if it ended with .html and set the loop variable

 RewriteCond %{ENV:REDIRECT_LOOP} !1 # if we didn't just added .html
 RewriteRule ^(.*)\.html$ https://%{HTTP:Host}/$1 [L,R=permanent] # then 301 redirect the request to the request without the .html

</Directory>

这将使得如果您有example.htmlexample/index.html,那么example.html将永远不会被加载。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51690012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档