www. 重定向 (.htaccess) 出现问题

www. 重定向 (.htaccess) 出现问题

我正在尝试设置一个简单的 www. 到非 www. 重定向。这是 .htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.org [NC]
RewriteRule ^(.*)$ http://domain.org/$1 [L,R=301]

RewriteBase /

########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below

## This attempts to block the most common type of exploit `attempts` to Joomla!
#

## Deny access to extension xml files (uncomment out to activate)
#<Files ~ "\.xml$">
#Order allow,deny
#Deny from all
#Satisfy all
#</Files>


## End of deny access to extension xml files
# Block out any script trying to set a mosConfig value through the URL
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]

#
########## End - Rewrite rules to block out some common exploits


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

访问 www.domain.org 页面会导致页面未找到。如果我像这样修改顶部几行:

RewriteCond %{HTTP_HOST} ^domain.org [NC]
RewriteRule ^(.*)$ http://domain123.org/$1 [L,R=301]

重定向效果很好(它转到http://domain123.org

apache httpd.conf 文件(其中配置了虚拟主机,特别是 domain.org)中没有针对 domain.org 的 RewriteCond。

apachectl-S:

VirtualHost configuration:
xxx.xxx.xxx.xxx:443    domain.org (/etc/httpd/conf.d/ssl.conf:173)
xxx.xxx.xxx.xxx:80     domain.org (/etc/httpd/conf/httpd.conf:1011)
Syntax OK

每个配置的相关部分: ssl.conf:

<VirtualHost xxx.xxx.xxx.xxx:443>

DocumentRoot /blah/blah/
ServerName domain.org

ErrorLog /blah/blah/
CustomLog /blah/blah/
LogLevel warn
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /blah/blah/
SSLCertificateKeyFile /blah/blah/
SSLCACertificateFile /blah/blah/

<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

SetEnvIf User-Agent ".*MSIE.*" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

CustomLog logs/ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

http.conf:

<VirtualHost xxx.xxx.xxx.xxx:80>

    ServerName domain.org
    ServerAdmin [email protected]
    DocumentRoot /blah/blah/
    ErrorLog /blah/blah/
    CustomLog /blah/blah/

</VirtualHost>

非常感谢帮助!

答案1

我们的 DNS 中没有 www.domain.com 的条目。添加该条目解决了问题(或者至少将该流量路由到我的服务器,我在该服务器上实施了上述重定向)。

谢谢你的评论。

相关内容