Trying to do a simple redirect:
rewrite https://url.example.com(.*) https://example.com/plugins/url permanent;
Anytime url.example.com is hit, I want it to redirect to that specific path.
EDIT:
Will try to explain this better, as I'm trying to redirect to a specific domain from another.
server {
server_name example.com plugin.example.com;
root /home/www/example.com/public;
}
I see the location used for redirects such as:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
But not sure how to use it in my case, which is to change plugin.example.com to example.com/plugin.
For example:
http://plugin.example.com
https://plugin.example.com
https://plugin.example.com/blah
https://plugin.example.com/blah/more
All of these should redirect to:
https://example.com/plugin
If the original URL is https://url.example.com or https://url.example.com/ then the normalized URI used by the rewrite and location directives will be /. The scheme, host name and query string have all been removed.
To perform a permanent redirect to a URL with a different host name:
Using rewrite (see this document for details):
rewrite ^/$ https://example.com/foo permanent;
Or using location and return (see this document for details):
location = / {
return 301 https://example.com/foo;
}
The second solution is more efficient, as there are no regular expressions to process.
If the original URL includes a query string: The rewrite will append it automatically unless a trailing ? is added. The return will not, but can be added by appending $is_args$args.
If the scheme and host name are unchanged, then both statements can be simplified:
rewrite ^/$ /foo permanent;
Or:
location = / {
return 301 /foo;
}
Redirect from subdomain to subfolder on main site
Do you require a redirect from a subdomain to a subfolder on the main site?
This would be best accomplished by a separate server context, with the appropriate server_name specification.
Else, you could also do this with an if statement testing against $host.
As already pointed out elsewhere, rewrite directive operates based on $uri, which does not contain the hostname.
server_name-based matching (recommended):
Hardcoded redirect with a limited number of hostnames (recommended):
server {
server_name plugin.example.com;
return 301 $scheme://example.com/plugin$request_uri;
}
server {
server_name about.example.com;
return 301 $scheme://example.com/about$request_uri;
}
Regex-based redirect from any subdomain to the main domain:
server {
server_name ~^(?:www\.)?(?<subdomain>.*)\.example\.com$;
return 301 $scheme://example.com/$subdomain$request_uri;
}
Regex-based redirect from a limited number of subdomain to the main domain:
server {
server_name ~^(?:www\.)?(?<subdomain>plugin|about)\.example\.com$;
return 301 $scheme://example.com/$subdomain$request_uri;
}
if-based:
If-statement-based redirect with hardcoded hostnames:
server {
server_name .example.com;
…
if ($host = plugin.example.com) {
return 301 $scheme://example.com/plugin$request_uri;
}
if ($host = about.example.com) {
return 301 $scheme://example.com/about$request_uri;
}
…
}
If-statement-based redirect with a regex-based matching:
server {
server_name .example.com;
…
if ($host ~ ^(?:www\.)?(?<subdomain>plugin|about)\.example\.com$) {
return 301 $scheme://example.com/$subdomain$request_uri;
}
…
}
Please refer to http://nginx.org/r/server_name for more discussion of which option may be best for you.
You can create separate servers for example.com and plugin.example.com. And create redirect inside plugin.example.com server.
server {
server_name plugin.example.com;
return 301 https://example.com/plugin;
}
server {
server_name example.com;
root /home/www/example.com/public;
}
Related
I have a WordPress site that needed to 301 redirect old post to new post.
Old post:
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
New Post:
https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/
I added this rule in nginx.conf for this domain here
server
{
listen 111.222.333.444:80;
server_name example.com www.example.com ;
return 301 https://www.example.com$request_uri;
}
server
{
rewrite_log on;
rewrite ^/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/$ https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/ permanent;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
(the rest of location blocks continue)
}
Restart Nginx.
However, the old URL still return 404 and not a 301.
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
And I don't see neither old nor new URI in error log at all. What should I do? Thanks!
The percent encoded URL is available in the $request_uri variable. But by the time Nginx is processing rewrite and location statements, the URL has been decoded and normalised.
Use a rewrite or location statement with the decoded values. For example:
rewrite ^/สวัสดี/$ /ลาก่อน/ permanent;
Or:
location = /สวัสดี/ {
return 301 /ลาก่อน/;
}
I am currently facing a small problem using nginx to redirect to another host. I want to for example redirect https://service.company.com/new/test.html to https://new-service.company.com/test.html .
For now I have following configuration, which redirects me to https://new-service.company.com/new/test.html .
server {
# SSL
ssl_certificate /etc/nginx/cert/chained_star_company.com.crt;
ssl_certificate_key /etc/nginx/cert/star_company.com.key;
listen 443;
server_name service.company.com;
location /new/$1 {
return 301 $scheme://service-new.company.com/$1;
}
}
I also tried following with the same result:
return 301 $scheme://service-new.company.com/$request_uri
You want to rewrite the URI and redirect. You can achieve it using location and return directives, but a rewrite directive would be the simplest approach:
rewrite ^/new(.*)$ https://new-service.company.com$1 permanent;
See this document for more.
BTW, the problem with your location block solution, was the regular expression capture, wasn't. Use:
location ~ ^/new(.*)$ {
return 301 https://new-service.company.com$1$is_args$args;
}
See this document for more.
I have looked at this question which describes how to redirect a url in nginx, as shown below
# main server block for www.test.com
server {
listen 80;
server_name www.test.com;
...
}
# redirect test.com to www.test.com
server {
server_name test.com;
return 301 $scheme://www.test.com$request_uri;
}
What I need to do is redirect a set of individual pages, so was wondering how to do this
e.g. test.com\index , test.com\home , test.com\main to test.com\index.php
Then I have some other pages to simply redirect simply to the .php extension
e.g. test.com\about to \test.com\about.php
e.g. test.com\contact to \test.com\contact.php
What is the best way to do this?
Found the answer... assuming the following server block for test.com
server {
listen 80;
server_name www.test.com;
...
}
Add the appropriate regex location path, and rewrite or return to the redirect url.
for test.com\index , test.com\home , test.com\main to test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
for test.com\about to \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}
I have been tasked with a couple project.
We have two directories on our server, one is
http://example.com/app
and the other is
http://example.com/fw
What I have been asked to do, is redirect from http to https if any visitor lands on a page in these two directories (app and fw)
Here is what I have done so far in the config file. When I added the lines to my config file below 'server' and restarted the site would not come back up. Unfortunately I don't have access to the log files. Appreciate anyone willing to take a look at this
location ~ ^/miner/memclub/.+\.html$ {
rewrite ^(.+)\.html$ /bootstrap.php?file=$1.html last;
rewrite ^(.+)\.phtml$ /bootstrap.php?file=$1.phtml last;
error_page 404 = /404.php;
}
server {
server_name site.org;
server_name *.site.org;
location /app {
if ( $scheme = http ) {
rewrite ^ https://site.org/app last;
}
}
}
First of all I don't think you can have 2 server_name, merge those two lines into one line
server_name example.com *.example.com;
And to do the https redirect i would recommend using 2 separate servers, you need one listening to port 443 anyway
server {
server_name example.com www.example.com; # which ever you are using
listen 443 ssl;
location / {
# all your https configuration
}
}
server {
server_name example.com www.example.com:
listen 80;
location /app {
return 301 https://$http_host$request_uri;
}
location /fw {
return 301 https://$http_host$request_uri;
}
location / {
# the rest of the non https configuration
}
}
I know you can merge both app and fw into one location, but I believe doing it without regex is faster, if you want to do it anyways here it is
location /(app|fw) {
return 301 https://$http_host$request_uri;
}
Each nginx config can act for a wide range of domains but I want to auto-redirect requests to the first domain name (the official one).
server {
server_name a.example.com b.example.com;
}
I want that if someone enters b.example.com/some, to go directly to a.example.com/some
This is pretty much the same thing as the GOOD example for http://wiki.nginx.org/Pitfalls#Server_Name. That is, you should use two servers:
server {
server_name b.example.com;
return 301 $scheme://a.example.com$request_uri;
# For pre-0.8.42 installations:
# rewrite ^ $scheme://a.example.com$request_uri? permanent;
}
server {
server_name a.example.com;
# Do stuff
}
To do this in a single server block, you can use an if and the $server_name variable:
server_name primary.tld secondary.tld;
if ($host != $server_name) {
rewrite ^ $scheme://$server_name permanent;
}
Or, to keep any query parameters:
server_name primary.tld secondary.tld;
if ($host != $server_name) {
rewrite ^/(.*) $scheme://$server_name/$1 permanent;
}
Here, $server_name refers to primary server name, which is the first name in the server_name directive, while $host refers to the hostname given in the HTTP request.
Note that the if statement in nginx configuration does not always do what you would expect and its use is discouraged by some. See also https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
This answer was inspired by this answer to another question which uses a similar aproach.
Combined version of #kolbyjack and #Matthijs answers with one server block. This config will redirect all requests with Host header different from example.com and process only example.com requests.
server {
server_name example.com a.example.com b.example.com;
if ($host != $server_name) {
return 301 $scheme://$server_name$request_uri;
}
# processing requests to $server_name (example.com) only
...
}