Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


NGINX: Redirecting Folder to Subdomain and strip folder name
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

NGINX: Redirecting Folder to Subdomain and strip folder name

MrAndroidMrAndroid Member
edited August 2011 in Help

Im trying to redirect example.com/sub to sub.example.com, however all the rewrite rules I try seem to make example.com/sub/$1 goto sub.example.com/sub/$1

Im using this to do the redirect.

http://www.landofdaniel.com/pastie/p/505326.txt

Comments

  • MrAndroidMrAndroid Member
    edited August 2011

    Trying

               rewrite ^/sub(.*) http://sub.example.com$1 permanent; 
    

    however, example.com/sub goes to sub.example.com/sub while example.com/sub/$1 goes to sub.example.com/$1

  • http://serverfault.com/questions/269881/nginx-redirect-subfolder-to-subdomain

    ??

    Which would make it:

    location ^~ /~sub/ {
    rewrite ^/~sub(.*) http://sub.example.com$1 permanent;
    }

    That's slightly different than what you have. There's a ~ mark in front of the 2 sub bits.

    Not sure if that would help in this case but worth a try. :)

  • As soon as you replied DrMike, I think I figured it out.

               rewrite ^/sub/(.*) http://sub.example.com/$1 permanent;
    

    Appears to work, if theres an issue with that, let me know.

  • drmikedrmike Member
    edited August 2011

    No idea actually although thanks for the response. You're the one with the issue though so if it works for you....

    edit: I hate rewrite rules.

  • drmike said: it: I hate rewrite rules.

    Same here, considering Apache's "Rewrite" rules are a total different thing

  • SpotVPSSpotVPS Member
    edited August 2011

    In order to prevent that internal rewrite and this new external redirect that you wish to add from interfering with each other to create an 'infinite' rewrite/redirect loop, you must check that the request for the subdomain's subdirectory is a direct request from the client (browser) and not the result of your original subdomain-to-subdirectory internal rewrite. This checking can be done using THE_REQUEST which holds the original client HTTP request header:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub/

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

Sign In or Register to comment.