【问题标题】:Multiple redirects in .htacces (http to https; .html to .php; index to /).htaccess 中的多个重定向(http 到 https;.html 到 .php;索引到 /)
【发布时间】:2019-07-10 17:36:11
【问题描述】:

用于重定向所有不带“www”的请求。或使用“http:”我在激活 SSL 后使用以下 .htaccess 设置:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^index\.php/(.+)$ /$1 [R=301,L]
RewriteRule ^index\.php/?$ / [R=301,L]

我现在注意到,如果请求的 URL 是 http,我以前将 index.php 重定向到 / 不再起作用。

另外,我现在需要将当前的 .html 文件(我网站的一半)更改为 .php 并添加:

RewriteRule ^(.*)\.html$ $1.php [L]

我想使用尽可能少的重写来最大化 SEO 效果,但我在尝试时遇到了困难。任何帮助将不胜感激。

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    在尝试了几个小时之后,我想我找到了一个优雅的解决方案:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^(.*)index\.php/?$ [OR]
    RewriteCond %{REQUEST_URI} ^(.*)index\.html/?$
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ [R=301,L]
    # index.php or index.html are sent to https://www.domain.tld/
    
    RewriteCond %{REQUEST_URI} ^(.*)\.html$
    RewriteRule ^(.*)\.html$ https://www.%{HTTP_HOST}/$1.php [R=301,L]
    # .html is changed to .php and additionally redirected to https://www.
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    # Everthing not redirected before, gets an additional www. and/or is redirected to https.
    

    我很高兴 cmets 和建议,如有必要,可以进一步改进。希望这对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2012-11-02
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多