nginx url rewriting between 2 domain names - wordpress

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 :)

Related

301 Redirect on nginx

I'm doing a 301 redirect from old domains to the new domain but can't able to exactly make it work.
So just a TLD change and everything else is same.. Like URL
Old domain : https://donateers.com
New domain https://donateers.org
Here is the code I tried in the nginx file but got 503 error.
server {
. . .
server_name donateers.com;
rewrite ^/(.*)$ https://donateers.org/$1 permanent;
. . .
}
The site is using the easy engine stack
Thanks
Suresh
Solution:
The above code works, Just I need to add in the main.conf file
Just below the code of
server_name donateers.com;
I need to add the following 1 line of code to do the 301 redirect.
rewrite ^/(.*)$ https://donateers.org/$1 permanent;
We're making the redirection permanent here and every URL of old domain change to the new URL with 301 redirection. Even works with http, www version of donateers.com
After you add this.. You need to restart the start with the one line of command.
ee site restart donateers.com
EasyEngine makes it very handy.
use this
server {
listen 80;
listen 443;
server_name .donateers.com;
return 301 $scheme://donateers.org$request_uri;
}

Nginx - Redirect only main domain and not subdomain to WWW

How can I setup a redirection in Nginx conf to only redirect the main domain and not subdomain to WWW using $host or $server_name?
Any help will be appreciated.
server {
server_name your_domain_without_www;
location / {
rewrite ^(.*)$ http://your_domain_with_www$1 permanent;
}
}
server {
server_name *.your_domain_without_www;
...
}
You could also split the second server section into 2 separate ones if you need to handle www.your_domain and other subdomains of your domain differently.
Use .htacess file to redirect the url this will solve your problem

WordPress blog nginx proxy issue

I am running a blog on blog.example.com. I am using nginx as proxy for example.com/blog. I would like to change word press site URL and word press URL . When i change site URL preview of post breaks. When i change word press URL WP-admin breaks. When i access example.com/blog/WP-admin it gives a 404 error.
I have tried replacing all the values in database dump file to example.com/blog. But it didn't help. Also tried to proxy all php file under wp-admin from example.com/blog/ to blog.example.com, Nothing worked.
Anyone can please suggest a solution here.?
Thanks
I cannot exactly know what you mean. Would you mind offering your configuration file here?
And I think your configuration file for nginx proxy would be the code below:
server {
listen 80;
server_name example.com;
location / {
# other configurations
}
location /blog {
rewrite . /blog/ redirect;
}
location /blog/ {
proxy_pass http://blog.example.com/;
# the '/' suffix in the url is important
proxy_set_header X-Forwarded-For $proxy_add_forwarded_for;
# and any other configs
}
}

Redirecting from subdomain to folder on Dotcloud

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;
}

How to redirect any request to a specific page if the visitor was using a certain name?

How can I get visitors redirected to a specific html page if the name used to resolve the server address was a specific one? I tried
if ($http_host ~ /forbiddenname/)
{
rewrite ^(.*)$ /updateyourlinks.html break;
}
inside the Server section, but doesn't work...
After some research I found that I should use instead of an if a virtual host... what I've added to nginx configuration is another server
server {
listen 80;
server_name *.badname.com;
rewrite ^ http://goodname.com/updateyourlinks.html;
}
and this apparently works

Resources