# Redirecting existing links to a sub-folder to a new subdomain

**URL:** <https://www.ruby-forum.com/t/redirecting-existing-links-to-a-sub-folder-to-a-new-subdomain/234296>\
**Category:** NGINX\
**Created:** [February 22, 2015, 9:31am UTC](https://www.ruby-forum.com/t/redirecting-existing-links-to-a-sub-folder-to-a-new-subdomain/234296 "2015-02-22T09:31:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [February 22, 2015, 9:31am UTC](https://www.ruby-forum.com/t/redirecting-existing-links-to-a-sub-folder-to-a-new-subdomain/234296/1 "2015-02-22T09:31:53Z")

</div>

If I want to transform links from  
[http://exampledomain.com/sub-folder/some-article](http://exampledomain.com/sub-folder/some-article) to  
[http://subdomain.exampledomain.com/some-article](http://subdomain.exampledomain.com/some-article) where subdomain has the  
same  
name as the subfolder, do I need a rewrite or a redirect? I _think_ I  
need a  
redirect but want to be sure I am doing it the recommended way.

I tried “rewrite /sub-folder-name/(.\*)  
$scheme://sub-folder-name.exampledomain.com/$1 permanent;”

But this didn’t seem to have any effect after restarting nginx.

Thanks!

Posted at Nginx Forum:

> **[Redirecting existing links to a sub-folder to a new subdomain](https://forum.nginx.org/read.php?2,256869,256869#msg-256869)**
>
> If I want to transform links from http://exampledomain.com/sub-folder/some-article to http://subdomain.exampledomain.com/some-article where subdomain has the same name as the subfolder, do I need a rewrite or a redirect? I \*think\* I need a redirect...

---

<div class="post-metadata">

**Author:** ![codecowboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@codecowboy](https://www.ruby-forum.com/u/codecowboy)\
**Post date:** [February 22, 2015, 4:48pm UTC](https://www.ruby-forum.com/t/redirecting-existing-links-to-a-sub-folder-to-a-new-subdomain/234296/2 "2015-02-22T16:48:28Z")

</div>

On Sun, Feb 22, 2015 at 03:30:58AM -0500, codecowboy wrote:

Hi there,

> If I want to transform links from  
> [http://exampledomain.com/sub-folder/some-article](http://exampledomain.com/sub-folder/some-article) to  
> [http://subdomain.exampledomain.com/some-article](http://subdomain.exampledomain.com/some-article) where subdomain has the same  
> name as the subfolder, do I need a rewrite or a redirect? I _think_ I need a  
> redirect but want to be sure I am doing it the recommended way.

You need to issue a http redirect.

You can do that using the nginx directive “rewrite” or the nginx  
directive  
“redirect” (among other ways).

> I tried “rewrite /sub-folder-name/(.\*)  
> $scheme://sub-folder-name.exampledomain.com/$1 permanent;”

That can work, depending on where exactly it is written.

> But this didn’t seem to have any effect after restarting nginx.

What do the logs show? “Nothing” suggest one kind of fix; “something”  
suggests another, depending on what the “something” is.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![codecowboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@codecowboy](https://www.ruby-forum.com/u/codecowboy)\
**Post date:** [February 22, 2015, 7:28pm UTC](https://www.ruby-forum.com/t/redirecting-existing-links-to-a-sub-folder-to-a-new-subdomain/234296/3 "2015-02-22T19:28:21Z")

</div>

Hi,

Thanks for your reply. I got this working. Here’s what I ended up with  
in  
case it helps someone else:

“rewrite /student-area/(.\*) $scheme://studentarea.mydomainname.com/$1  
permanent;”

This was placed outside of any location {} blocks but within the server  
{}  
block.

Posted at Nginx Forum:

> **[Re: Redirecting existing links to a sub-folder to a new subdomain](https://forum.nginx.org/read.php?2,256869,256873#msg-256873)**
>
> If I want to transform links from http://exampledomain.com/sub-folder/some-article to http://subdomain.exampledomain.com/some-article where subdomain has the same name as the subfolder, do I need a rewrite or a redirect? I \*think\* I need a redirect...
