6

我在httpd.conf重定向中使用以下代码:

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

但是,它最终重定向到www.domain.com/domain.com//domain.com//domain.com

我希望以下所有 URL 重定向到 domain.com:

http://domain.com
http://www.domain.com
www.domain.com
4

4 回答 4

10

这应该这样做。问题是它看起来很像你已经拥有的。当您http://domain.com在浏览器中输入时,它是否有效?还是重定向到其他地方?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com$1 [R=permanent,L]
于 2012-05-19T01:23:21.233 回答
1

http://www.webweaver.nu/html-tips/web-redirection.shtml建议

Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
于 2012-05-19T01:23:04.953 回答
1
RewriteEngine On

# Redirect ANY non-www request to https and www
RewriteCond %{HTTP_HOST} !^(www.\.domain\.com)?$

RewriteRule (.*) https://www.domain.com/$1 [R=301,L]

# Redirect ANY HTTP request to https and www
RewriteCond %{SERVER_PORT} !443

RewriteRule (.*) https://www.domain.com/$1 [R=301,L] 
于 2014-06-28T10:41:37.950 回答
0

对于 SSL 虚拟主机,请使用以下内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]

下面的完整示例:

<VirtualHost *:443>
    ServerAdmin youname@yourdomain.com
    DocumentRoot "/var/www/html/yourdomain"
    ServerName yourdomain.com
    ServerAlias  www.yourdomain.com *.yourdomain.com
    ErrorLog "logs/yourdomain-error_log"
    CustomLog "logs/yourdomain-access_log" common
    ErrorDocument 404 /index.php
    <Directory "/var/www/html/yourdomain">
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/yourdomain/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/yourdomain/chain.pem
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
    RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]
</VirtualHost>
于 2019-01-07T09:59:04.823 回答