.htaccess - 301 redirect all files without extension to have .html extension

不羁岁月 提交于 2019-12-12 11:28:59

问题


I need to 301 redirect requests to files with no extension to same with appended .html extension:

http://www.mydomain.com/this

to

http://www.mydomain.com/this.html

The following would not get redirected:

http://www.mydomain.com/that/ (it's a directory)
http://www.mydomain.com/other.php

Any help appreciated on the above, thanks.


回答1:


Try the following. I would place it as the last rule in your set (i.e. the bottom) to not conflict with any other rules.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\..+$
RewriteRule ^(.*)$ /$1.html [R=301,L]

This should ensure the request is not a directory and that is doesn't end with some kind of extension. If those conditions are met, it will append the request with .html.

This is untested, so comment back if it works. ;)



来源:https://stackoverflow.com/questions/4337548/htaccess-301-redirect-all-files-without-extension-to-have-html-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!