【问题标题】:redirect all the pages from non www to www in htaccess在htaccess中将所有页面从非www重定向到www
【发布时间】:2017-04-21 15:31:22
【问题描述】:

我知道有很多关于从非 www 到 www 域的重定向的信息。但我的问题是我可以管理主页的重定向,但不能管理子页面。

您可以看到示例here 因此,当我输入类似ourenglishclass.eu/fill-in-text-5th-6th-grade 的网址时,重定向会发生在www.ourenglishclass.eu/index.php

我可以看到可能有更多的重写规则导致它以这种方式运行,但我找不到什么,或者我该如何解决这个问题

这些是重定向到/index.php的规则

 #
 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
 #
 # If the requested path and file is not /index.php and the request
 # has not already been internally rewritten to the index.php script
 RewriteCond %{REQUEST_URI} !^/index\.php
 # and the requested path and file doesn't directly match a physical file
 RewriteCond %{REQUEST_FILENAME} !-f
 # and the requested path and file doesn't directly match a physical folder
 RewriteCond %{REQUEST_FILENAME} !-d
 # internally rewrite the request to the index.php script
 RewriteRule .* index.php [L]

我已经尝试过这些从非 www 到 www 的重定向规则:

  #RewriteCond %{HTTP_HOST} !^www\. [NC]
  #RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

并且(这是因为我也在使用 https 进行测试)

  # Redirect to www
  RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
  RewriteCond %{HTTPS}s ^on(s)|
  RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

希望有人能指出出路

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    问题似乎是由于您将重定向规则置于前端控制器规则下方,该规则将所有内容转发到 index.php,因此使 REQUEST_URI 等于 /index.php

    你的规则是这样的:

    RewriteEngine On
    
    # Redirect to www
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    #
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    #
    # If the requested path and file is not /index.php and the request
    # has not already been internally rewritten to the index.php script
    RewriteCond %{REQUEST_URI} !^/index\.php
    # and the requested path and file doesn't directly match a physical file
    RewriteCond %{REQUEST_FILENAME} !-f
    # and the requested path and file doesn't directly match a physical folder
    RewriteCond %{REQUEST_FILENAME} !-d
    # internally rewrite the request to the index.php script
    RewriteRule ^ index.php [L]
    

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

    【讨论】:

    • 非常感谢您的解释和解决方案!这行得通!
    猜你喜欢
    • 2018-11-25
    • 2012-03-08
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2014-07-26
    • 2011-04-12
    • 2015-12-31
    相关资源
    最近更新 更多