在htaccess中将所有页面从非www重定向到www

时间:2017-04-21 15:31:23

标签: apache .htaccess redirect mod-rewrite

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

你可以看到例子here所以当我输入网址如ourenglishclass.eu/fill-in-text-5th-6th-grade时,重定向就发生在www.ourenglishclass.eu/index.php < / p>

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

这些是重定向到/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]

希望有人可以指明出路

1 个答案:

答案 0 :(得分: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]

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