Remove file extension with .htaccess: Error with trailing slash

限于喜欢 提交于 2019-12-25 04:06:12

问题


I want to remove the file extension like .html from my websites with .htaccess. The final structure should be like so:

http://domain.com/file  --> http://domain.com/file.html
http://domain.com/file/ --> http://domain.com/file.html

With my existing code in .htaccess I'll get "Internal Server Error" on my Browser when there's a trailing slash at the end. What can I do? Thanks!

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

回答1:


RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-_]+)/?$ $1.html [L]



回答2:


I suggest you to change your RewriteCond :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^(.*)(\.html){0}$ /$1.html [L] 

EDIT : rule edited, I forgot infinite loop.



来源:https://stackoverflow.com/questions/11036848/remove-file-extension-with-htaccess-error-with-trailing-slash

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