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.
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
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.
Comments
Trying
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.
Appears to work, if theres an issue with that, let me know.
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.
Same here, considering Apache's "Rewrite" rules are a total different thing
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]