我在apache主机文件中具有以下内容,以从www重定向到网站的非www版本:
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
我还想重定向任何人转到该URL:
www.domain.com/page/amp => domain.com/page/
问题在于,如果我在.htaccess中添加以下规则,则会导致额外的重定向:
RewriteCond %{REQUEST_URI} (.+)/amp(.*)$ [NC]
RewriteRule ^ %1/ [R=301,L]
发生的事情是,当任何人去www.domain.com/page/amp
时,它都会进行两次重定向:
首先:www.domain.com/page/amp
=> 301到=> domain.com/page/amp
第二个:domain.com/page/amp
=> 301到=> domain.com/page
如何修改vhosts文件中的规则以最小化这种额外的重定向?
理想情况下发生的示例网址:
www.domain.com => domain.com
www.domain.com/page => domain.com/page
www.domain.com/page/amp => domain.com/page
上面的最后一个是我要最小化重定向的目的。
更新:
这两个都是虚拟主机(用于端口80和443):
<VirtualHost *:80>
DocumentRoot /var/www/html
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/html
ServerName domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.domain.com
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>