Redirecting from subdomain to folder on Dotcloud - nginx

Is it possible to redirect from a subdomain to a folder using the application nginx.conf on Dotcloud?
I want to redirect http://blog.domain.com to http://www.domain.com/blog
I may be wrong but as I understand the content of the application level nginx.conf is inserted into a server block of the main nginx.conf.
I've tried the following directive in the nginx.conf but it doesn't work:
rewrite http://blog.domain.com http://www.domain.com/blog/ permanent;
Has anyone else managed to do this?

Ok, so I figured this out. Whilst Ifs are considered evil in nginx this was the best I could do:
if ($http_host = "blog.domain.com") {
rewrite ^/(.*)$ http://www.domain.com/blog/$1?$args;
}

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

WP migration from apache to nginx results -- 404 -- on https

That was kind of a loaded title.
I moved my site over from Apache to Nginx and I'm learning the hard way that Nginx does things its own ways. It does not like htaccess files, ignores them. This causes problems when running Wordpress, because wordpress supposedly loves to use htaccess files and nginx does not. Why? I have no idea.
Anyways,
I managed to figure out how to bring back the site from the abyss of 404, by placing this code in nginx.conf file
location / {
index index.php index.html;
if (!-e $request_filename)
{
rewrite ^/(.+)$ /index.php last;
}
}
But.
while pages load fine on HTTP, HTTPS still shows dreaded 404. Why? I don't know. So, anybody know what to do next?
Well, it turns out I also have to add the same code on nginx.ssl.conf file.
Why? I don't know, but it works.

Redirecting old url to Nginx with param

I have an old URL structure like this archives.php?l=e and I moved it to a new structure archives/e
I'm trying to 301 redirect all the old urls, but I have run into troubles. This is what I'm currently doing but it's not working.
location ^~ /list.php {
rewrite /archives/$1/ permanent;
}
Any help would be greatly appreciated.
I figured it out and I have to say Nginx has some nice magic for this. Using arg_l solved it. Somehow it creates a variable for each param.
location ^~ /archives.php {
rewrite ^/archives.php$ /archives/$arg_l? permanent;
}

Rewrite a URL in nginx without redirecting

I'm trying to create a rewrite with nginx that will change the displayed URL without actually redirecting to it. EG: http://example.com/item/rest-of-path would become http://example.com/folder/rest-of-path. I've been working with different variations of this code with in my nginx.conf:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 last;
}
but that doesn't seem to be doing the trick. Any ideas where I'm going wrong here? I'll admit I'm still pretty new to server-side rewrites in general.
Try this:
location ~ /example {
rewrite ^/example/(.*) /folder/$1 break;
}
use break.
Try this. It isn't necessary to place it under the location block, and don't forget to reload nginx.
rewrite ^/example/(.+)$ /folder/$1 last;

nginx url rewriting between 2 domain names

Got an issue with URL rewrite under nginx. Actually I have a main domain name dns1.com which is pointing a webserver with a WordPress installation (permalink = /%category%/%postname%/). I've created this page dns1.com/forum/forumname1.
I also have a domain name forumname1.com and I want this one to be pointed to the content of dns1.com/forum/forumname1. The issue is that I don't know how to display the result of dns1.com/forum/forumname1 with only forumname1.com in the bar address :( Actually it works when I go to forumname1.com/forum/forumname1 but this URL is pretty redundant :/ I should make this parameter "/forum/forumname1" hidden but how?
Here is my nginx serverblocks :
server{
server_name dns1.com;
root /var/www/dns1.com;
location = /forum/forumname1 {
rewrite ^/(.*)$ http://forumname1.com permanent;
}
}
server{
server_name forumname1.com;
root /var/www/dns1.com;
location = / {
### Rewriting rule HERE but which one?
}
}
Thank you :)

Resources