【问题标题】:301 redirects(.htaccess) - http to https + www to non-www + 'index.php' to '/'301 重定向(.htaccess) - http 到 https + www 到非 www + 'index.php' 到 '/'
【发布时间】:2019-08-12 15:08:28
【问题描述】:

我浏览了很多关于 301 重定向 (.htaccess) 的文章,但没有找到任何可以帮助解决所有 3 个重定向的文章,正如我在这个问题的标题中提到的那样(http 到 https + www 到非www + 'index.php' 到 '/')。

到目前为止我所尝试的(但无法达到预期的结果):

.htaccess file

RewriteEngine on

#redirect http to https plus www to non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
#redirect /index.php to /
#RewriteRule ^index\.php$ / [L,R=301]

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 year"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

这工作正常,只有当涉及到httphttps,但所有三个重定向都不能同时工作,如果我尝试不同的解决方案,我会得到Error Too many redirects

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    可以使用下面的htaccess

    RewriteEngine on
    
    #redirect http to https plus www to non-www
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
    #redirect /index.php to /
    RewriteRule ^index\.php$ / [L,R=301]
    

    编辑:

    如果您使用的是 cloudflare,那么您需要将 %{HTTPS} 变量替换为 Cloudflare url 方案检查变量 %{HTTP:CF-Visitor}

    替换此行

    RewriteCond %{HTTPS} off [OR]
    

    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
    

    确保在测试此更改之前清除浏览器缓存。

    【讨论】:

    • 这就是我所做的@starkeen
    • @RaviWadhwani 你在使用 Cloudflare 吗?
    • 是的!我正在使用 cloudflare。
    • 现在我也想从 URL 中删除 .php,我已经看到了很多解决方案,但我担心,如果我这样做,我可能会遇到 https & www 重定向错误的问题。请帮忙! @starkeen
    【解决方案2】:

    试试这个:

    RewriteEngine on 
    
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^  https://example.com%{REQUEST_URI} [L,R=301]
    RewriteRule ^index\.php / [R=301,L]
    

    上面的规则将匹配https://wwwhttp://www 以及所有http:// 并强制它们进入https://without-wwww 并且也将仅忽略https://without-www

    最后一行将删除 URI 中的 index

    注意:清除浏览器缓存测试

    【讨论】:

    • @RaviWadhwani 你清除浏览器缓存了吗?你还有其他规则吗?粘贴您的 htaccess 文件
    • 是的,在检查解决方案是否有效之前,我确实清除了缓存/cookies!我已经在我的问题中添加了 htaccess 文件!
    猜你喜欢
    • 2019-02-04
    • 2017-05-03
    • 2018-10-19
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多