[英]Redirect http to https and www to non-www in .htaccess
First of all, I know there are lots of answers on this, but I don't actually find one that works.首先,我知道对此有很多答案,但实际上我没有找到一个有效的答案。 This is what I have in the
.htaccess
file right now, and I want to mention that it worked previously, but it does not anymore.这就是我现在在
.htaccess
文件中的内容,我想提一下它以前有效,但现在不再有效。
Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [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]
# FORCE HTTPS AND NON WWW
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As a mention, I will have a lot of Redirect 301 from old pages to the new ones since the entire structure has been changed.顺便提一下,由于整个结构已更改,因此我将有很多从旧页面到新页面的重定向 301。
And the links that I am redirecting inside my website come with "www" like:我在网站内重定向的链接带有“www”,例如:
https://www.example.com/unt-de-cacao-de-plaja/filtre/producator/crisnatur/
and needs to be redirected to:并且需要重定向到:
https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
A few issues, in order of importance:几个问题,按重要性排序:
You have your canonical HTTP to HTTPS and www to non-www redirects at the end of the file.您在文件末尾有规范的 HTTP 到 HTTPS 和 www 到非 www 的重定向。 By placing it at the end of the file, after your front-controller , it's simply never going to be processed for most requests.
通过将它放在文件的末尾,在您的front-controller 之后,它永远不会被大多数请求处理。 This needs to be near the start of the
.htaccess
file, before your front-controller .这需要在
.htaccess
文件的开头附近,在您的 front-controller之前。
You should avoid mixing redirects from both mod_alias ( Redirect
) and mod_rewrite ( RewriteRule
) in the same scope.您应该避免在同一范围内混合来自 mod_alias (
Redirect
) 和 mod_rewrite ( RewriteRule
) 的Redirect
。 Different modules execute at different times throughout the request, despite their apparent order in the config file.不同的模块在整个请求中的不同时间执行,尽管它们在配置文件中的顺序明显。 Since mod_rewrite is required for other redirects, you should convert the mod_alias
Redirect
directives to use RewriteRule
instead.由于其他重定向需要 mod_rewrite,您应该将 mod_alias
Redirect
指令转换为使用RewriteRule
。
For example:例如:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
You should include the canonical scheme and hostname in your URL redirects in order to avoid multiple redirects when requesting an "old" URL at a non-canonical scheme ot hostname.您应该在 URL 重定向中包含规范方案和主机名,以避免在以非规范方案或主机名请求“旧”URL 时进行多次重定向。
For example:例如:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
Depending on what you mean exactly by "a lot of Redirect 301" - you should not be doing this at all in .htaccess
and instead redirecting in your server-side script, once you have determined that the request will 404. This is to prioritise normal site visiters and not your redirects (that get executed on every single request).根据您所说的“大量重定向 301”的确切含义,您根本不应该在
.htaccess
执行此操作,而应在服务器端脚本中进行重定向,一旦您确定请求将是 404。这是优先考虑的普通站点访问者而不是您的重定向(在每个请求上都会执行)。
RewriteCond %{ENV:HTTPS} !on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] RewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Since you stated that these directives worked previously then I assume the use of the HTTPS
environment variable is OK on your system.既然你说这些指令以前有效,那么我假设在你的系统上使用
HTTPS
环境变量是可以的。 But note that, whilst this is relatively common, it's non-standard .但请注意,虽然这是相对常见的,但它是非标准的。 (It implies the server is using some kind of SSL front-end/proxy.)
(这意味着服务器正在使用某种 SSL 前端/代理。)
Note that the order of these rules will result in a double redirect when requesting http://www.example.com/<anything>
(HTTP + www).请注意,在请求
http://www.example.com/<anything>
(HTTP + www) 时,这些规则的顺序将导致双重重定向。 Which is necessary if you are implementing HSTS , but otherwise, you should reverse these two rules to avoid this unnecessary double redirect.如果您正在实施HSTS ,这是必要的,否则,您应该颠倒这两个规则以避免这种不必要的双重重定向。
Redirect to https and non-www
重定向到 https 和非 www
To instead redirect all requests to https and non-www, use the following code instead of the previous:要将所有请求重定向到 https 和非 www,请使用以下代码而不是之前的代码:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site.和以前一样,将此代码放在您网站的根目录 .htaccess 中。 Here is what it's doing:
这是它在做什么:
Checks if mod_rewrite is available检查 mod_rewrite 是否可用
Checks if HTTPS is off, or if the request includes www检查 HTTPS 是否关闭,或者请求是否包含 www
If either condition matches, the request qualifies and is redirected to the https/non-www address如果任一条件匹配,则请求符合条件并重定向到 https/非 www 地址
OR或者
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.