【问题标题】:"Too many redirects" when try to redirect site using htaccess尝试使用 htaccess 重定向站点时出现“重定向过多”
【发布时间】:2015-07-24 04:16:47
【问题描述】:

我需要使用 301 重定向将我的网站从 example.net 重定向到 www.example.net

我正在为我的网站使用 All In One WP Security & Firewall,它会自动在我的 .htaccess 文件中添加其他代码。重定向不起作用,我认为这是由额外的代码引起的。我在浏览器中看到的错误是:

尝试打开 www.example.net 时发生了太多重定向。

All In One WP Security & Firewall会产生这样的错误吗?

我的 301 代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.net [NC]
RewriteRule ^(.*)$ www.example.net/$1 [L,R=301,NC]

多合一代码:

# BEGIN All In One WP Security
#AIOWPS_BLOCK_WP_FILE_ACCESS_START
<Files license.txt>
order allow,deny
deny from all
</files>
<Files wp-config-sample.php>
order allow,deny
deny from all
</Files>
<Files readme.html>
order allow,deny
deny from all
</Files>
#AIOWPS_BLOCK_WP_FILE_ACCESS_END
#AIOWPS_BASIC_HTACCESS_RULES_START
<Files .htaccess>
order allow,deny
deny from all
</Files>
ServerSignature Off
LimitRequestBody 10240000
<Files wp-config.php>
order allow,deny
deny from all
</Files>
#AIOWPS_BASIC_HTACCESS_RULES_END
#AIOWPS_PINGBACK_HTACCESS_RULES_START
<Files xmlrpc.php>
order deny,allow
deny from all
</Files>
#AIOWPS_PINGBACK_HTACCESS_RULES_END
#AIOWPS_DISABLE_INDEX_VIEWS_START
Options -Indexes
#AIOWPS_DISABLE_INDEX_VIEWS_END
#AIOWPS_IP_BLACKLIST_START
Order allow,deny
Allow from all
#AIOWPS_IP_BLACKLIST_END
#AIOWPS_DISABLE_TRACE_TRACK_START
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
#AIOWPS_DISABLE_TRACE_TRACK_END
#AIOWPS_FORBID_PROXY_COMMENTS_START
RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP:VIA}%{HTTP:FORWARDED}%{HTTP:USERAGENT_VIA}%{HTTP:X_FORWARDED_FOR}%{HTTP:PROXY_CONNECTION} !^$ [OR]
RewriteCond %{HTTP:XPROXY_CONNECTION}%{HTTP:HTTP_PC_REMOTE_ADDR}%{HTTP:HTTP_CLIENT_IP} !^$
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteRule .* - [F,NS,L]
#AIOWPS_FORBID_PROXY_COMMENTS_END
#AIOWPS_DENY_BAD_QUERY_STRINGS_START
RewriteCond %{QUERY_STRING} tag=     [NC,OR]
RewriteCond %{QUERY_STRING} ftp:     [NC,OR]
RewriteCond %{QUERY_STRING} http:    [NC,OR]
RewriteCond %{QUERY_STRING} https:   [NC,OR]
RewriteCond %{QUERY_STRING} mosConfig [NC,OR]
RewriteCond %{QUERY_STRING} ^.*(globals|encode|localhost|loopback).* [NC,OR]
RewriteCond %{QUERY_STRING} (\;|'|\"|%22).*(request|insert|union|declare|drop) [NC]
RewriteRule ^(.*)$ - [F,L]
#AIOWPS_DENY_BAD_QUERY_STRINGS_END
#AIOWPS_ADVANCED_CHAR_STRING_FILTER_START
<IfModule mod_alias.c>
RedirectMatch 403 \,
RedirectMatch 403 \:
RedirectMatch 403 \;
RedirectMatch 403 \=
RedirectMatch 403 \@
RedirectMatch 403 \[
RedirectMatch 403 \]
RedirectMatch 403 \^
RedirectMatch 403 \`
RedirectMatch 403 \{
RedirectMatch 403 \}
RedirectMatch 403 \~
RedirectMatch 403 \"
RedirectMatch 403 \$
RedirectMatch 403 \<
RedirectMatch 403 \>
RedirectMatch 403 \|
RedirectMatch 403 \.\.
RedirectMatch 403 \%0
RedirectMatch 403 \%A
RedirectMatch 403 \%B
RedirectMatch 403 \%C
RedirectMatch 403 \%D
RedirectMatch 403 \%E
RedirectMatch 403 \%F
RedirectMatch 403 \%22
RedirectMatch 403 \%27
RedirectMatch 403 \%28
RedirectMatch 403 \%29
RedirectMatch 403 \%3C
RedirectMatch 403 \%3E
RedirectMatch 403 \%3F
RedirectMatch 403 \%5B
RedirectMatch 403 \%5C
RedirectMatch 403 \%5D
RedirectMatch 403 \%7B
RedirectMatch 403 \%7C
RedirectMatch 403 \%7D
# COMMON PATTERNS
Redirectmatch 403 \_vpi
RedirectMatch 403 \.inc
Redirectmatch 403 xAou6
Redirectmatch 403 db\_name
Redirectmatch 403 select\(
Redirectmatch 403 convert\(
Redirectmatch 403 \/query\/
RedirectMatch 403 ImpEvData
Redirectmatch 403 \.XMLHTTP
Redirectmatch 403 proxydeny
RedirectMatch 403 function\.
Redirectmatch 403 remoteFile
Redirectmatch 403 servername
Redirectmatch 403 \&rptmode\=
Redirectmatch 403 sys\_cpanel
RedirectMatch 403 db\_connect
RedirectMatch 403 doeditconfig
RedirectMatch 403 check\_proxy
Redirectmatch 403 system\_user
Redirectmatch 403 \/\(null\)\/
Redirectmatch 403 clientrequest
Redirectmatch 403 option\_value
RedirectMatch 403 ref\.outcontrol
# SPECIFIC EXPLOITS
RedirectMatch 403 errors\.
RedirectMatch 403 config\.
RedirectMatch 403 include\.
RedirectMatch 403 display\.
RedirectMatch 403 register\.
Redirectmatch 403 password\.
RedirectMatch 403 maincore\.
RedirectMatch 403 authorize\.
Redirectmatch 403 macromates\.
RedirectMatch 403 head\_auth\.
RedirectMatch 403 submit\_links\.
RedirectMatch 403 change\_action\.
Redirectmatch 403 com\_facileforms\/
RedirectMatch 403 admin\_db\_utilities\.
RedirectMatch 403 admin\.webring\.docs\.
Redirectmatch 403 Table\/Latest\/index\.
</IfModule>
#AIOWPS_ADVANCED_CHAR_STRING_FILTER_END
#AIOWPS_BLOCK_SPAMBOTS_START
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^(.*)?wp-comments-post\.php(.*)$
RewriteCond %{HTTP_REFERER} !^http(s)?://example\.net\.au [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule .* http://127.0.0.1 [L]
</IfModule>
#AIOWPS_BLOCK_SPAMBOTS_END
#AIOWPS_PREVENT_IMAGE_HOTLINKS_START
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://example\.net\.au [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
</IfModule>
#AIOWPS_PREVENT_IMAGE_HOTLINKS_END
# END All In One WP Security

【问题讨论】:

  • 您能在.htaccess 文件中发布代码示例吗?
  • @Quill 我已添加代码。
  • 我删除了问题末尾的“谢谢”,因为它不会为问题添加任何内容。写标题时,提醒自己没有人来 StackOverflow 询问有关完美运行的代码的问题。当人们遇到类似问题时,他们会使用标题来查找与他们的问题相似的问题。使用标题中出现的部分错误而不是“不工作”对他们和可以回答您问题的人更有用。
  • 使用RELOCATE 方法来反映此更改,否则.htaccess 和WordPress 将相互重定向,从而创建您正在经历的重定向循环
  • @anubhava 你能帮我解决这个问题吗?

标签: wordpress .htaccess mod-rewrite


【解决方案1】:

.htaccess 文件中的重定向

.htaccess 文件是一个配置文件,用于修改网站/服务器上每个目录的 Apache 服务器行为。这是一个用户级配置文件,这里只能编辑部分Apache配置,虽然重定向是常用的。

您可以将多个 .htaccess 文件级联到一系列目录中。如果您在父目录中有一个 .htaccess,在子目录中有另一个,它们都会影响子目录。在这些情况下,请务必记住您在哪里拥有和不拥有 .htaccess 文件,以防止不同级别的 .htaccess 文件之间发生冲突。

以下是一系列重定向示例,有助于识别 .htaccess 文件中的重定向。这些不是进行此类重定向的唯一方法,但它们应该向您展示最常见的重定向是什么样的,以便您可以识别它们是否在您正在使用的 .htaccess 文件中。

强制 HTTPS 下面的 .htaccess 代码首先检查请求是使用 HTTP 还是 HTTPS 进入服务器的。如果请求未使用 HTTPS,则配置将告诉浏览器重定向到之前请求的同一网站和 URL 的 HTTPS 版本。

RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

强制 HTTPS: 在负载均衡器或代理(CloudFlare/Incapsula/Sucuri/等)后面时 有时您可能正在使用代理,例如负载均衡器或 Web 防火墙,例如 CloudFlare、Incapsula 或 Sucuri。这些可以配置为在前端使用 SSL,但不能在后端使用 SSL。为了使其正常工作,您不仅需要检查请求中的 HTTPS,还需要检查代理是否仅使用 HTTP 将原始 HTTPS 请求传递给服务器。以下规则检查请求是否是从 HTTPS 转发的,如果是,则不会尝试重定向额外的时间。

RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteCond %{HTTP:X-Forwarded-Proto} =http
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

强制非www 此重定向仅检查是否在域名开头使用 www 请求网站名称。如果包含 www,它会重写请求并告诉浏览器重定向到非 www 版本的域名。

RewriteEngine On
 RewriteCond %{HTTP_HOST} ^www\. [NC]
 RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

强制 www 最后一个重定向检查是否没有在域名开头使用 www 请求网站名称。如果不包含 www,它会重写请求并告诉浏览器重定向到 www 版本的域。

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

WordPress WordPress CMS 使用 .htaccess 文件将 URL 重写为 index.php 文件,但它将网站的 URL 定义为数据库中的值。如果您还不知道网站上正在使用的数据库的名称,您可以在 WordPress 的主配置 (wp-config.php) 中查找它。

您也可以在文本编辑器中打开该文件并查找这些值,但是通过 SSH 连接,您可以使用程序 grep。这不仅为您提供了数据库名称,而且数据库名称对于我们接下来需要做的事情来说是最重要的。

grep DB wp-config.php

define('DB_NAME', 'wordpress_database');
 define('DB_USER', 'wordpress_username');
 define('DB_PASSWORD', 'Password');
 define('DB_HOST', 'localhost');
 define('DB_CHARSET', 'utf8');
 define('DB_COLLATE', '');

wp_options 表 一旦您知道数据库的名称,您就可以查看 WordPress 数据库的选项表以查看 URL 在数据库中设置的内容。选项表的表名开头可以有任何前缀,但默认情况下通常是“wp_”,因此选项表的全名通常是wp_options。重要的两行是选项表中的 home 和 siteurl 行。您可以使用 phpMyAdmin 或其他数据库管理实用程序找到这些,但在命令行中,您也可以只运行以下 mysql 命令。

mysql -e 'show tables' wordpress_database | grep options

prefix_options

检查配置的 URL 从命令行,您可以检查选项表中 home 和 siteurl 行的当前值。该命令应该向您发送并输出如下示例。重要的部分是这些在大多数情况下相互匹配,并且它们是您所期望的。如果它们不是您所期望的,那么您将需要相应地更新它们。

mysql -e 'select * from wp_options where option_name rlike "home|siteurl"' wordpress_database
 +-----------+-------------+----------------------------------+----------+
 | option_id | option_name | option_value                     | autoload |
 +-----------+-------------+----------------------------------+----------+
 |        36 | home        | http://www.example.com           | yes |
 |         1 | siteurl     | http://www.example.com           | yes |
 +-----------+-------------+----------------------------------+----------+

更新配置的 URL 以下命令会将给定数据库的 wp_options 表的两行更新为新 URL。此命令应该适合大多数需要更新或更正为 WordPress 站点配置的 URL 的情况。更新 WordPress Multisite 中配置的 base_urls 超出了本文的范围,但会涉及更新多个 wp_option 类型的表。

mysql -e 'update wp_options set option_value="https://www.example.com" where option_name rlike "home|siteurl"' wordpress_database

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。问题不在于规则。规则是正确的,但 .htaccess 中规则的顺序不正确。这是我以前的。注意我正在重写为非www。

    #https
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
      #remove home
      RewriteCond %{THE_REQUEST} ^.*/home
      RewriteRule ^(.*)home$ https://example.com/$1 [R=301,L] 
    
    
     # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
     #  slashes.
     # If your page resides at
     #  http://www.example.com/mypage/test1
     # then use
     # RewriteBase /mypage/test1/
     #RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php?/$1 [L]
    
       #301 redirect
     RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
     RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    

    我改成

     #301 redirect
     RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
     RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    
     #https
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
      #remove home
      RewriteCond %{THE_REQUEST} ^.*/home
      RewriteRule ^(.*)home$ https://example.com/$1 [R=301,L] 
    
    
     # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
     #  slashes.
     # If your page resides at
     #  http://www.example.com/mypage/test1
     # then use
     # RewriteBase /mypage/test1/
     #RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php?/$1 [L]
    

    【讨论】:

      【解决方案3】:

      问题出在

      RewriteRule ^(.*)$ www.example.net/$1 [L,R=301,NC]
      

      www.example.net 被视为相对路径,而不是不同的域。

      这就是为什么它将URLhttp://example.net/some/path重写为http://example.net/www.example.net/some/path,然后又重写为http://example.net/www.example.net/www.example.net/some/path等等。

      如果您想从域 example.net 重写为域 www.example.net,您还必须添加协议 (http),例如

      RewriteRule ^(.*)$ http://www.example.net/$1 [L,R=301,NC]
      

      另一种重定向域名批发的方法是使用Redirect 指令

      Redirect / http://www.example.net
      

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

      使用%{HTTP_HOST} 具有相同的效果,因为它不包含方案,即httphttps。此外,您从%{HTTP_HOST} 重写为%{HTTP_HOST},这并没有改变。

      要解决此问题,您必须在替换前加上 www. 和一个方案,httphttps。这里不需要标志NC,因为无论如何你都在寻找任何字符.*。所以规则应该看起来像

      RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R]
      

      最后,永远不要使用R=301 进行测试!有关详细信息,请参阅此答案Tips for debugging .htaccess rewrite rules

      【讨论】:

      • 我试过了。但同样的问题。这是我的代码。 RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L,R=301,NC]
      猜你喜欢
      • 2016-08-13
      • 2018-10-21
      • 1970-01-01
      • 2023-02-24
      • 2023-04-02
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多