Apache 2.4.6配置:部分页面重定向至新域名,部分保留原服务器
域名部分页面重定向配置问题解决
我正在做域名迁移,需要把旧域名http://olddomain.com的部分页面重定向到新域名http://newdomain.com,剩下的页面留在原服务器。站点是基础HTML/PHP架构,只通过httpd.conf配置,不用.htaccess。
具体需求:
- 访问
http://olddomain.com/时,返回原服务器的index.html - 访问
http://olddomain.com/contact-us.html时,返回原服务器对应页面 - 访问
olddomain.com/service.html时,重定向到http://newdomain.com
我的原始httpd.conf内容:
ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost <Directory /> AllowOverride none Require all denied </Directory> DocumentRoot "/var/www/html" <Directory "/var/www"> AllowOverride None Require all granted </Directory> <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html index.htm index.php </IfModule> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/error_log" LogLevel warn <IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> CustomLog "logs/access_log" combined </IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" </IfModule> <Directory "/var/www/cgi-bin"> AllowOverride None Options None Require all granted </Directory> <IfModule mime_module> TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml </IfModule> AddDefaultCharset UTF-8 <IfModule mime_magic_module> MIMEMagicFile conf/magic </IfModule> EnableSendfile on
两次失败的尝试
第一次尝试
配置如下,结果是所有页面都被重定向:
<VirtualHost *:80> ServerName olddomain.com ServerAlias www.olddomain.com DocumentRoot /var/www/sites/olddomain DirectoryIndex index.html # 本来想的是如果URL不含index.html或contact-us.html就重定向到新域名,结果所有页面都跳了 <IfModule mod_rewrite.c> RewriteEngine on RewriteRule !index\.html$ http://newdomain.com%{REQUEST_URI} [L,R] RewriteRule !contact-us\.html$ http://newdomain.com%{REQUEST_URI} [L,R] </IfModule> </VirtualHost>
第二次尝试
配置如下,结果是规则完全被忽略:
<VirtualHost *:80> ServerName olddomain.com ServerAlias www.olddomain.com DocumentRoot /var/www/sites/olddomain DirectoryIndex index.html # 本来想的是如果URL不含index.html或contact-us.html就重定向到新域名,结果规则没生效 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond "%{REQUEST_URI}" "! /index.html" [OR] RewriteCond "%{REQUEST_URI}" "! /contact-us.html" [OR] RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] </IfModule> </VirtualHost>
正确配置方案
下面是能满足需求的VirtualHost配置,直接替换你之前的旧域名虚拟主机配置即可:
<VirtualHost *:80> ServerName olddomain.com ServerAlias www.olddomain.com DocumentRoot /var/www/sites/olddomain DirectoryIndex index.html <IfModule mod_rewrite.c> RewriteEngine on # 仅当请求URI既不是根路径(对应index.html)也不是contact-us.html时,才重定向 RewriteCond %{REQUEST_URI} !^/$ RewriteCond %{REQUEST_URI} !^/contact-us\.html$ RewriteRule ^(.*)$ http://newdomain.com$1 [R=301,L] </IfModule> </VirtualHost>
错误原因解释
第一次尝试的问题:
两个RewriteRule是独立执行的,第一个规则匹配所有不是index.html的请求(包括contact-us.html),直接触发重定向并终止后续规则([L]标记),所以contact-us.html也被跳走了。第二次尝试的问题:
- 条件用了
[OR],逻辑完全错误:只要满足“不是/index.html”或者“不是/contact-us.html”就跳转,而所有请求至少满足其中一个,导致应该保留的页面也被跳了; - 正则表达式里的空格(
! /index.html)会导致匹配失败,因为请求URI里没有这个空格; - 没有处理根路径
/的情况,根路径访问时REQUEST_URI是/,不是/index.html,所以需要单独排除。
- 条件用了
内容的提问来源于stack exchange,提问作者James Connigan
相关产品推荐
相关产品推荐

