gpt4 book ai didi

linux - 挣扎于 .htaccess(Godaddy Linux 主机)

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:37 24 4
gpt4 key购买 nike

非常感谢您在以下方面的帮助。我一直在通过试错来谷歌搜索和测试代码,我相信我正在慢慢弄清楚事情,但我会感谢在结果和我的学习发展方面的快速 channel 帮助。

我目前通过 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]

最佳答案

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

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]

关于linux - 挣扎于 .htaccess(Godaddy Linux 主机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com