从网址中删除.html扩展名

前端之家收集整理的这篇文章主要介绍了从网址中删除.html扩展名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试通过编辑web root(public_html)目录下的.htaccess文件从我的网站的url中删除.html扩展名.我的服务器是bluehost.

问题是我在.htaccess文件中已有先前的设置,用于将主域重定向到子目录.

脚本如下.还有我在网上发现的用于从网址隐藏扩展名的脚本.(脚本不起作用)如果有人可以帮我解决问题,那将是非常好的.谢谢.

# Use PHP5.4 Single PHP.ini as default
AddHandler application/x-httpd-PHP54s .PHP


# BlueHost.com 
# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on 
# Change example.com to be your main domain. 
RewriteCond %{HTTP_HOST} ^(www.)?qinglish.ca$
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteCond %{REQUEST_URI} !^/qinglish_ca/ 
# Don't change the following two lines. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Change 'subdirectory' to be the directory you will use for your main domain. 
RewriteRule ^(.*)$/qinglish_ca/$1 
# Change example.com to be your main domain again. 
# Change 'subdirectory' to be the directory you will use for your main domain 
# followed by / then the main file for your site,index.PHP,index.html,etc. 
RewriteCond %{HTTP_HOST} ^(www.)?qinglish.ca$
RewriteRule ^(/)?$qinglish_ca/index.html [L]

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html !-f
RewriteRule ^([^\.]+)$$1.html [NC,L]

解决方法

您需要使用以下规则来删除所有网址的.html扩展名,而不是您的上一条规则:
## hide .html extension
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]

# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$/$1.html [L]

将它们放在您有当前规则的相同位置以添加.html扩展名.

猜你在找的HTML相关文章