【问题标题】:remove .html extension from url从 url 中删除 .html 扩展名
【发布时间】:2015-03-20 21:03:43
【问题描述】:

我正在尝试通过编辑 web root(public_html) 目录下的 .htaccess 文件从我的网站的 url 中删除 .html 扩展名。我的服务器是bluehost。

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

脚本如下。还有我在网上找到的用于从 url 隐藏扩展名的脚本。(该脚本不起作用)如果有人能帮助我解决这个问题,那就太好了。谢谢。

# 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 扩展名仍然显示

标签: html .htaccess


【解决方案1】:

您将需要这些规则来删除所有 URL 的 .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 扩展的相同位置。

【讨论】:

  • 您好,抱歉,您提供的脚本一切正常,但是,我刚刚发现我在同一域下的其他子目录页面现在存在链接问题。就像这个页面的链接停止工作一样。 linklink。有没有办法修复它? @anubhava
  • 您好,我不确定我放的对不对,所以请到这个link告诉我,谢谢:)
  • 没有链接中的图片是我所有的规则,我从其他子目录文件夹中删除了所有其余的 .htaccess 文件,只保留了文档根目录中的一个,但问题仍然存在。
  • 但是http://www.qinglish.ca/cn/portfolio-item-02.html 现在加载正常,虽然它没有删除.html 扩展。你能检查你的VirtualHost 配置并检查DocumentRoot 设置的内容吗?
  • 您好,我查看了一些虚拟主机视频,真的不明白,并弄清楚如何检查虚拟主机和文档根设置为...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-03-04
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多