2015-05-13 59 views
3

我需要使用301重定向將我的站點從example.net重定向到www.example.net嘗試使用htaccess重定向站點時出現「重定向過多」

我使用所有在一個WP安全&防火牆爲我的網站,它的自動添加我.htaccess文件中額外的代碼。重定向不起作用,我認爲這是由額外的代碼引起的。我在瀏覽器中看到的錯誤是:

重定向過多發生試圖打開www.example.net

請問所有在一個WP安全&防火牆產生這樣的錯誤?

我的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 
+0

您可以發佈您'.htaccess'文件中的代碼示例? – Quill

+1

@Quill我添加了代碼。 – Sree

+0

我在問題結尾刪除了「謝謝」,因爲它沒有在問題中添加任何內容。在編寫標題時,請提醒自己,沒有人來StackOverflow提出有關完美工作的代碼的問題。當人們遇到類似的問題時,他們會使用標題找到看起來像他們問題的問題。使用標題中的部分錯誤而不是「不工作」對於他們和能夠回答您的問題的人來說更有用。 – Sumurai8

回答

2

問題是與

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

www.example.net被視爲相對路徑,而不是一個不同域。

這是什麼原因,爲什麼它重寫URL http://example.net/some/pathhttp://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

+0

我試過了。但同樣的問題。這是我的代碼。 ! RewriteEngine敘述在 RewriteBase/ 的RewriteCond%{HTTP_HOST}^WWW \ $ [NC] 重寫規則^(。*)$ HTTP。(*)://www.% {HTTP_HOST} 1/$ [L,R = 301,NC] – Sree

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]