I have a nginx config file that currently handles rewriting of requests via index.php for a Zend Framework application. Example code as follows:
server {
listen 80 default_server
etc....
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^/(.*)$ /index.php/$1 last;
}
etc....
}
However, I now need to add a redirect so that any non-www requests for any domain are processed with a 301 to the www equivalent.
I've come across examples such as:
server {
listen 80;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
But how would I combine everything so that any non-www are first redirected with a 301 and then rewritten via index.php?
Two separate server blocks (exactly as you have in your question). The first server block is the default, which handles the www prefix domains already, and (presumably) handles the index.php rewrite correctly.
The second server block only matches domain names without the www prefix.
If you wanted to do it the other way around, that is rewrite index.php before you redirect, then just add a rewrite line to the new server block.
Related
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;
}
I have a front-end application (in reactjs) and a static blog that I want to display from the same server through nginx. My application is accessible at the root of the domain / and my blog is currently accessible on a subdomain blog.domain.com. But I'm currently switching from a subdomain to the /blog subfolder on the same domain and I have an issue while displaying the blog from the subfolder. The problem only appears on chrome and is quite inconsistent. What happens is that when browsing /blog I get a 404 from the js application, not the static site (the css styles are different so it's easy to spot who responded). If I refresh the page without cache (ctrl + shift + R) I get a response from static blog. If I click on a link, again I get a 404 from the js page and if I clean-refresh now I get the correct page from the blog. Any f5 on a page redirect to a 404 from the js page.
In firefox or private navigation, everything seems fine though so I was thinking about a common cache issue in the browser at first but the fact that after dropping the cache I still get 404s from the wrong directory makes me think about some parameters that would be sent and not treated the same way in one case or another.
I have noticed that ctrl-shift-R redirects me to /blog/ from /blog when the blog is displayed properly but an f5 on /blog/ still gives a 404 from the js frontend. It might mean that the 301 to the trailing slash does something more. But a ctrl-shift-R on /blog/ does not trigger a 301 (just a 200) with the expected website displayed.
Here is my nginx config file for this site:
server {
listen 443 http2 default_server;
listen [::]:443 http2 default_server;
root /opt/react_folder/build;
index index.html;
server_name domain.com www.domain.com;
location / {
try_files $uri $uri/ /index.html =404;
}
location /blog {
alias /opt/blog_subfolder;
try_files $uri $uri/ /index.html =404;
}
... backend block
... ssl block
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
rewrite ^ https://$host$request_uri? permanent;
}
I have redesigned a website and changed the url formats too.
Now i need to change the old url to new one.
Here is my old url:
http://www.example.com/forum/showPost/2556/Urgent-Respose
The new url will be:
http://www.example.com/2556/Urgent-Respose
How to redirect to new url using nginx by removing /forum/showPost from url?
Edited:
Also this url:
http://www.tikshare.com/business/showDetails/1/Pulkit-Sharma-and-Associates,-Chartered-Accountants-in-Bangalore
New url:
http://www.tikshare.com/classifieds/1/Pulkit-Sharma-and-Associates,-Chartered-Accountants-in-Bangalore
Above link is complete removing whereas this link is to replace business/showDetails with classifieds
There are a number of options. You could protect the rewrite within a location block which would be quite efficient as the regular expression is only tested if the URI prefix matches:
location ^~ /forum/showPost {
rewrite ^/forum/showPost(.*)$ $1 permanent;
}
See this document for more.
You used permanent in your question - which generates a 301 response.
If you use redirect instead of permanent - a 302 response will be generated.
If you use last instead of permanent - an internal redirect will occur and the browser address bar will continue to show the old URL.
In response to your comment:
rewrite ^/forum/showPost(.*)$ /post$1 permanent;
server
{
listen 80; ## Listen on port 80 ##
server_name example.com; ## Domain Name ##
index index.html index.php; ## Set the index for site to use ##
charset utf-8; ## Set the charset ##
location ^~ /forum/showPost {
rewrite ^/forum/showPost(.*)$ $1 permanent;
}
location ^~ /business/showDetails {
rewrite ^(.*)business/showDetails(.*)$ classifieds$1 permanent;
}
}
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
I have a problem.
My app server is nginx, on which my blog was hosted.
when i visited my sitemap with this url:
http://www.ikbear.com/sitemap.xml, it works.But when i visited my sitemap with this url:
http://ikbear.com/sitemap.xml, it doesn't work. So i want to redirect http://ikbear.com/sitemap.xml to http://www.ikbear.com/sitemap.xml, would you tell me how can i do that in nginx? Thanks!
Actually I'm going to venture a guess that you'll have the same trouble redirecting that url as actually serving it.
First, here's the syntax for a basic redirect:
server {
# ...
# redirect sitemap.xml to sitemap.xml.php
rewrite ^(/sitemap.xml)$ /sitemap.xml.php;
# ...
}
What might work for you is getting both www and not-www serving correctly. A common strategy is to serve all www to non-www, or vice versa. Here's an example of that:
server {
listen 80;
server_name www.mydomain.com;
# forward everything from www.domain.com to domain.com
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com *.domain.com;
location / {
root /var/www/domain/htdocs;
index index.html index.htm index.php;
# ... etc.
}
}