Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 redirect all pages/files (EXCEPT FOR ONE) to new domain

htaccess redirect except 1 html file

         

KenWeiLL

6:35 am on Jun 18, 2015 (gmt 0)

10+ Year Member



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


Is this even possible?

I have attached the full .htaccess file. However, the most relevant parts of the file are pasted below:

---------RELEVANT PARTS OF THE .HTACCESS FILE---------
RewriteEngine On

RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com.com$
RewriteRule ^(.*)\.html$ 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]

RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [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

whitespace

12:18 am on Jun 19, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Yes, this is possible.

Just a quick question though, is olddomain.com and newdomain.com hosted in the same webspace, or are they two entirely separate sites?

Also, I'm intrigued by your second RewriteRule (always trying to improve my regex-foo):

RewriteRule ^(.*)([^/])$ http://www.newdomain.com/$1$2/ [L,R=301]


Isn't this the same as:

RewriteRule ^(.*[^/])$ http://www.newdomain.com/$1/ [L,R=301]

? (To match a URL that doesn't end with a slash?)

lucy24

1:15 am on Jun 19, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To match a URL that doesn't end with a slash?

Watch out. If you set up a pattern like
(.*)/? or (.+)/?
then regardless of where you put your capturing parentheses, the / will always be included as part of the first group (the .* or .+). To force the / out of the first group while leaving it optional, it would have to be
^(.+[^/])/?$
Note that in this situation it doesn't need to be .* --assuming htaccess at the root-- because the server would never see a request consisting solely of a / slash. (Even if it's a malformed request like "http://example.com//" mod_rewrite probably won't see the extra slash.)

Now, personally I would never put RewriteRules for two different domains in the same htaccess. It's just too messy. Instead, give each domain its own htaccess with its own RewriteRules that are specific to that hostname. If you have multiple sites living in the same directory, you can still make a shared htaccess using mod_auththingy, mod_setenvif, ErrorDocument directives, expiration/caching rules, and almost anything else that has normal inheritance rules and isn't site-specific.

KenWeiLL

1:28 am on Jun 19, 2015 (gmt 0)

10+ Year Member



I've already found a solution that works, as I posted the same problem on different forums as I need to have a quick solution.

I emove the first and second rule and place this code at the top after 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]

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


Both of the codes above works.

As with $1$2, I don't know how that works but the previous freelancer put it that way and currently, I don't encounter a problem with that. So for now, I'll leave it that way.

Thanks.

lucy24

3:01 am on Jun 19, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What's the NE flag for? (That was a rhetorical question.)
I posted the same problem on different forums as I need to have a quick solution
Thanks for the warning. We'll remember that the next time you have a question. Matter of fact, it's part of my "why we make you do it yourself" boilerplate.

whitespace

11:54 am on Jun 19, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month




^(.+[^/])/?$



Note that in this situation it doesn't need to be .*


Although if you used .+ wouldn't that result in a match of at least 2 chars (as opposed to just 1)? The first being any char (.), which wouldn't be a slash (because of the request / mod_rewrite) and the second being anything other than a slash ([^/]) ?



RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]

This (and the other) pattern certainly simplifies it, but doesn't it change things a bit and prevents the following rules from being executed? Although that is probably intended as they all seem to be very similar?

Just a thought... If on Apache 2.4 would the following work at the top of your file (regardless of what directives followed):


RewriteRule ^googleverification\.html - [END]


?

lucy24

5:46 pm on Jun 19, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



wouldn't that result in a match of at least 2 chars

I guess if your site has URLs in the form example.com/a/ example.com/b/ et cetera, you really would have to use .* here ;)

A while back I had a flurry of malformed google requests for example.com/directory//subdir/pagename.html with a spurious double / slash. I never did figure out where it came from-- my typo or theirs, fortunately just in one directory-- but in the course of experimentation I found that the pattern
//
or
^/
isn't recognized in the body of a RewriteRule, only in a RewriteCond (meaning that, yes, I had to make a RewriteCond specifying REQUEST_URI with a positive match). I don't know if this applies to all Apache versions and/or all servers; it's only what I found experimentally in 2.2 on my particular host.

KenWeiLL

2:24 am on Jun 22, 2015 (gmt 0)

10+ Year Member



NE is for no encoding to handle special characters in URL

lucy24

3:48 am on Jun 22, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I did say it was a rhetorical question.