# Domain rewrite/redirect

**URL:** <https://www.ruby-forum.com/t/domain-rewrite-redirect/206488>\
**Category:** NGINX\
**Created:** [May 3, 2011, 12:36am UTC](https://www.ruby-forum.com/t/domain-rewrite-redirect/206488 "2011-05-03T00:36:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 3, 2011, 12:36am UTC](https://www.ruby-forum.com/t/domain-rewrite-redirect/206488/1 "2011-05-03T00:36:15Z")

</div>

I have two different domains. One is a shortened url for referrals. I  
want to be able redirect the host name from the short url with  
parameters to the actual url. For example:

[abcd.com/123456](http://abcd.com/123456)

rewrite/redirect to

[efgh.com/referrals/123456](http://efgh.com/referrals/123456)

Initially I used a CNAME setup for [adbcd.com](http://adbcd.com) to rewrite to [efgh.com](http://efgh.com) but  
that doesn’t rewrite the domain name or url.

Here’s a short version of my setup with the rewrite rules I’m trying to  
use:

http {  
server {  
listen 80;  
server\_name [abcd.com](http://abcd.com) [efgh.com](http://efgh.com);  
rewrite ^(.\*) https://$host$1 permanent;  
}

server {  
listen 443;  
server\_name [abcd.com](http://abcd.com) [efgh.com](http://efgh.com);  
if ($host ~\* ^(efgh.com)$ ) {  
rewrite ^/(.\*)$ [https://abcd.com/referrals/$](https://abcd.com/referrals/$) redirect;  
}  
}  
}

The url on the user’s browser after entering [https://efgh.com/123456](https://efgh.com/123456)  
should appear as [https://abcd.com/referrals/123456](https://abcd.com/referrals/123456). Currently, it’s  
just rewriting.

Posted at Nginx Forum:

> **[domain rewrite/redirect](https://forum.nginx.org/read.php?2,195075,195075#msg-195075)**
>
> I have two different domains. One is a shortened url for referrals. I want to be able redirect the host name from the short url with parameters to the actual url. For example: abcd.com/123456 rewrite/redirect to efgh.com/referrals/123456

---

<div class="post-metadata">

**Author:** ![chiefops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@chiefops](https://www.ruby-forum.com/u/chiefops)\
**Post date:** [May 3, 2011, 1:05am UTC](https://www.ruby-forum.com/t/domain-rewrite-redirect/206488/2 "2011-05-03T01:05:52Z")

</div>

I think I’m getting a bit further.

I modified this a bit and its redirecting but not carrying the  
parameters. Here’s the updated config:

http {  
server {  
listen 80;  
server\_name [abcd.com](http://abcd.com);  
rewrite ^(._) https://$host$1 permanent;  
}  
server {  
listen 80;  
server\_name [efgh.com](http://efgh.com);  
rewrite ^(._) [https://abcd.com/referrals/$1](https://abcd.com/referrals/$1) redirect;  
}  
server {  
listen 443;  
server\_name [abcd.com](http://abcd.com);  
}  
server {  
listen 443;  
server [efgh.com](http://efgh.com)  
rewrite ^(.\*) [https://abcd.com/referrals/$1](https://abcd.com/referrals/$1) permanent;  
}  
}  
}  
}

Posted at Nginx Forum:

> **[Re: domain rewrite/redirect](https://forum.nginx.org/read.php?2,195075,195084#msg-195084)**
>
> I have two different domains. One is a shortened url for referrals. I want to be able redirect the host name from the short url with parameters to the actual url. For example: abcd.com/123456 rewrite/redirect to efgh.com/referrals/123456

---

<div class="post-metadata">

**Author:** ![chiefops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@chiefops](https://www.ruby-forum.com/u/chiefops)\
**Post date:** [May 3, 2011, 9:45am UTC](https://www.ruby-forum.com/t/domain-rewrite-redirect/206488/3 "2011-05-03T09:45:16Z")

</div>

On Mon, 02 May 2011 19:55:11 -0400  
“chiefops” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> Hello,

You may also use HSTS headers to ensure your users use SSL instead of  
non-SSL. This won’t replace the redirect, but the compatible browser  
will know on the next visit that it should use the SSL server only. It  
won’t request the non-SSL part.

More about HSTS:

> **[HTTP Strict Transport Security](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security)**
>
> HTTP Strict Transport Security (HSTS) is a policy mechanism that helps to protect websites against man-in-the-middle attacks such as protocol downgrade attacks and cookie hijacking. It allows web servers to declare that web browsers (or other complying user agents) should automatically interact with it using only HTTPS connections, which provide Transport Layer Security (TLS/SSL), unlike the insecure HTTP used alone. HSTS is an IETF standards track protocol and is specified in .mw-parser-outp Th...

Cheers,

C.

---

<div class="post-metadata">

**Author:** ![chiefops](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@chiefops](https://www.ruby-forum.com/u/chiefops)\
**Post date:** [May 3, 2011, 1:55am UTC](https://www.ruby-forum.com/t/domain-rewrite-redirect/206488/4 "2011-05-03T01:55:23Z")

</div>

Nevermind, got if fixed on my end. Simple, just had to reread the wiki:

rewrite ^ [https://abcd.com/referrals$request\_uri](https://abcd.com/referrals$request_uri)? permanent;

Posted at Nginx Forum:

> **[Re: domain rewrite/redirect](https://forum.nginx.org/read.php?2,195075,195093#msg-195093)**
>
> I have two different domains. One is a shortened url for referrals. I want to be able redirect the host name from the short url with parameters to the actual url. For example: abcd.com/123456 rewrite/redirect to efgh.com/referrals/123456
