【问题标题】:Struggling with .htaccess (Godaddy Linux Hosting)与 .htaccess(Godaddy Linux 主机)苦苦挣扎
【发布时间】:2016-05-18 20:53:05
【问题描述】:

非常感谢您对以下内容的帮助。我一直在通过尝试错误来搜索和测试代码,我相信我正在慢慢解决问题,但我希望能在结果和我的学习发展方面提供快速帮助。

我目前通过 GoDaddy 在 apache Linux 服务器上的共享资源上托管了一个网站。我正在努力实现以下目标;-

1) 将非 www 流量重定向到 www。

2) 删除 .html 文件扩展名并在网址上添加尾部斜杠。

我目前有下面的代码,301 www。重定向正在工作,并且尾部斜杠也在工作。但是我在子页面上遇到 404 错误,即 www.swiftcomm.co.uk/contact/;-

如何用代码解决这个问题?

Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

【问题讨论】:

  • 您使用的大部分代码似乎有些不必要。 HTTPS 的 www 规则可以合二为一。而且您不需要对尾部斜杠进行 HTTPS 检查。但是,我的问题是:您想删除 html 扩展名并强制使用斜杠。 html 文件是否仍然存在,是否要重写它们?所以请求/contact/ 将服务于/contact.html。 (你在这里的意图不是很清楚,所以我需要问一下。)
  • 迈克,感谢您的回复。我最终试图强制执行,如果有人要输入 swiftcomm.co.uk/contact,它将重定向到 www.swiftcomm.co.uk/contact/,这将由根目录中的 contact.html 页面提供。我希望这更有意义?
  • 会的,一会儿就会回答。

标签: linux apache .htaccess redirect web-development-server


【解决方案1】:

请尝试以下代码 - 它可以替换您的整个文件。 cmets 解释了每个规则集的作用。

Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /

# Force www. prefix - http/https
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Remove .html extension suffix from URI only if it matches an existing file.
# Add trailing slash simultaneously.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s\/(.+)\.html\s [NC]
RewriteRule ^ %1/ [R=302,L]

# Force trailing slash if it has'nt been forced already.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^\/(.+[^/])$
RewriteRule ^ %{REQUEST_URI}/ [R=302,L]

# Rewrite non-existing files to their .html counterparts.
# If the .html file does not exist, Apache will gracefully degrade to a 404.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1.html [L]

【讨论】:

  • 迈克,再次感谢您的时间和帮助。我已将代码替换为 .htaccess 文件中的所有内容。我已经检查了网站和主页的加载是否正确,子页面有一些问题。 www.swiftcomm.co.uk/services 加载但没有任何外部内容(Java、CSS、图像)。我执行了一个检查元素,发现我收到了很多 ERR_TOO_MANY_REDIRECTS 和 ERR_EMPTY_RESPONSE。
  • ERR_EMPTY_RESPONSE 似乎是由于链接现在指向不正确的位置,例如 swiftcomm.co.uk/services/img/menu-bg.jpg 加载资源失败:这可能是由于 img 文件夹位于路由目录中,所以swiftcomm.co.uk/img/menu-bg.jpg
  • 您需要相对于站点的根目录引用您的资产。因此,只需在每个 src 之前添加一个 /
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多