Wolf87
Wolf87

Reputation: 540

How to redirect properly using htaccess?

I'm trying to redirect with rewrite rules in Apache - .htaccess. I had set of rules which worked fine for redirection all http:// to https://.

So if the user try http://www.example-old.com, http://example-old.com, http://www.example-new.com or http://example-new.com, all of this will be redirected to https://example-new.com

Now I want to add one more address on the server which I do NOT want to be redirected to https://example-new.com

So I want it to be excluded from the code below. If somebody try http://newest-example.com it should redirect to it properly.

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^ https://example-new.com%{REQUEST_URI} [L,R=301]

What I tried in addition to the code above is:

RewriteCond %{HTTP_HOST} ^newest-example\.com$ [NC]
RewriteRule ^ http://newest-example.com [R=301,S=1]

This code is above the first block I presented. Like you can see I tried to skip original RewriteRule with S=1 if URL is http://newest-example.com but that gives me this message:

Moved Permanently

The document has moved here.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Not that experienced with this and I'm not sure what I'm missing here. Thanks in advance.

Upvotes: 2

Views: 1770

Answers (1)

anubhava
anubhava

Reputation: 786349

You can add an exclusion rule in your first rule itself:

RewriteCond %{HTTP_HOST} !^newest-example\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC]
RewriteRule ^ https://example-new.com%{REQUEST_URI} [L,R=301]

Upvotes: 2

Related Questions