Htaccess - 从 http 重写为 https,将 www 重写为非 www [英] Htaccess - Rewrite from http to https and www to non-www

查看:19
本文介绍了Htaccess - 从 http 重写为 https,将 www 重写为非 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单 http 重写为 https,将 www 重写为非 www url.

I am trying to rewrite form http to https and www to non-www urls.

基本上,如果有人访问 http://www.example.com,他们需要重定向到 https://example.com 和所有其他错误 url 实例 (http://example.com 等).

Basically if someone goes to http://www.example.com they need to be redirected to https://example.com and all other instances of wrong urls (http://example.com, etc.).

我试图通过将建议拼凑在一起来做到这一点:

I tried to do this by piecing suggestions together:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www.garrysun.com [NC]
RewriteRule ^(.*)$ https://garrysun.com/$1 [L,R=301]

但是当我在重定向检查器中检查 URL 时,我收到错误... http://garrysun.com 有效很好,然后转到 https://garrysun.com 但是 http://www.garrysun.com 得到这个:

But when I check the URL in a redirect checker I get errors... http://garrysun.com works fine and goes to https://garrysun.com but http://www.garrysun.com gets this:

检查链接:http://www.garrysun.com重定向类型:301 永久移动重定向到:https://garrysun.com/https://www.garrysun.com

Checked link: http://www.garrysun.com Type of redirect: 301 Moved Permanently Redirected to: https://garrysun.com/https://www.garrysun.com

如何更正我的重写代码?

How can I correct my rewrite code?

附言- 我正在使用 OpenCart 1.5.4 并且我在新东西上方还有以下重写代码(不知道它是否会影响任何东西):

P.S. - I am using OpenCart 1.5.4 and I also have the following rewrite code above the new stuff (don't know if it affects anything):

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

推荐答案

您可以针对该需求使用此单一规则:

You can use this single rule for that requirement:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^ https://garrysun.com%{REQUEST_URI} [NE,L,R=301]

# rest of your rules will appear below

这篇关于Htaccess - 从 http 重写为 https,将 www 重写为非 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆