How can I .htaccess 301 redirect all pages/files (except for one) to new domain?

一曲冷凌霜 提交于 2019-12-12 21:22:14

问题


How can I .htaccess 301 redirect all pages/files (except for one) to new domain?

I have already made an .htaccess file that successfully redirects most all the different pages and files on my old domain (www.olddomain.com) to my new domain (www.newdomain.com).

However, I need one page to not be redirected to the new site, that page is the Google verification .html file:

http://www.olddomain.com/googleverification.html

I have attached the most relevant parts of the file (.htaccess) pasted below:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]

RewriteCond %{REQUEST_URI}  !.(php|html?|jpg|gif|png|pdf|tiff|bmp|ico|gz|zip|xml|txt)$
RewriteRule ^(.*)([^/])$ http://www.newdomain.com/$1$2/ [L,R=301]


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

回答1:


Keep your first rule as:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/googleverification\.html [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [L,R=301,NE]



回答2:


I've been asking the same question on different forums and another solution also works.

I just thought to share it here also.

RewriteCond {HTTP_HOST} !^www.newdomain.com$
RewriteCond %{REQUEST_URI} !^/googleverification.html
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]


来源:https://stackoverflow.com/questions/30911687/how-can-i-htaccess-301-redirect-all-pages-files-except-for-one-to-new-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!