尝试使用htaccess重定向网站时“重定向太多”

时间:2015-05-13 03:43:57

标签: wordpress .htaccess mod-rewrite

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

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

  

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

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

我的301代码:

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

All in One代码:

# 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

3 个答案:

答案 0 :(得分:2)

问题在于

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

www.example.net被视为相对路径,而不是另一个域。

这就是为什么它将网址http://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

答案 1 :(得分:0)

我遇到了同样的问题。问题不在于规则。规则是正确的但.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]

答案 2 :(得分:0)

.htaccess文件中的重定向

.htaccess文件是一个配置文件,用于修改网站/服务器上每个目录的Apache服务器行为。这是一个用户级的配置文件,尽管重定向很常见,但是在这里只能编辑一些Apache配置。

您可以拥有多个.htaccess文件,这些文件可以在一系列目录中级联。如果您在父目录中有一个.htaccess,在子目录中有另一个。它们都会影响子目录。在这些情况下,记住您在哪里以及没有.htaccess文件的位置非常重要,以防止不同级别的.htaccess文件之间发生冲突。

下面是一系列重定向示例,它们将帮助您识别.htaccess文件中的重定向。这些不是执行此类重定向的唯一方法,但它们应该向您展示最常见的重定向的外观,以便在它们位于正在使用的.htaccess文件中时识别出它们。

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

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