Nginx redirect setting - wordpress

My Nginx setting currently has this:
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect;
}
}
Basically for non-existing pages (404) it redirects user to the home page. But now I have a wordpress blog setup at https://domain.com/blog/, but any wordpress items eg. https://domain.com/blog/test also got redirected to the home page. I wonder how to fix this?

to do something different for requested url's whose path starts with '/blog/' add a corresponding location like so:
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect; }
}
location /blog/ {
#add in whatever directives are needed to serve your wordpress
}

You shouldn't be using an if. Please read the IfIsEvil page on the nginx wiki. Instead you should be using try_files.
Your config should look more like this:
location / {
try_files $uri $uri/ #notfound
}
location #notfound {
rewrite ^(.*)$ https://domain.com/index.php?id=$1 redirect;
}
Really, you shouldn't be doing this at all. Instead you should be setting a custom error page. Redirecting every 404 to the home page is bad for your SEO.
Edit: I just realized you were passing the URL into "id". So I removed my second comment about using . instead of ^(.*)$. Still an error page is your best bet. You can pick up the URL with $_SERVER['REQUEST_URI'] (if you avoid the hard redirect).
This page has some sample configurations that may be helpful to you. It has a few wordpress configs.

Related

nginx - Redirect to urls without extension and add slash at the end

Using nginx, I would like to redirect all my static html URLs( mydomain.com/index.html; mydomain.com/contact.html; mydomain.com/about.html ) to the same urls without extension but with a slash at the end.
The end urls should be like that:
mydomain.com/index/
mydomain.com/contact/
mydomain.com/about/
....
How can I achieve that? Thanks.
There are two parts to this question. The first is how to make the server return contact.html in response to the URI /contact/.
There are a number of ways to achieve this. For example:
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^(/.+)/$ $1.html last;
return 404;
}
Any URI ending with a / will be internally mapped to the same URI with the / replaced by .html.
The second part is if people are still accessing the .html URIs using the old scheme, you may want to externally redirect them to the new scheme.
The best way to achieve this without creating a redirection loop, is to check the original request stored in $request_uri. For example:
if ($request_uri ~* "\.html(?|$)") {
rewrite ^(.*)\.html $1/ permanent;
}

Silly nginx rewrite issue - infinate loop

I'm totally baffled with this one. Its probably something silly, and I'm missing it after along day! Anyway, I have this rewrite rule setup in my nginx config for the site:
location / {
root /srv/www/site.co.uk/www;
index index.html index.htm index.php;
rewrite ^/(.*)/index.html$ http://site.co.uk/$1/ permanent;
rewrite ^/index.html$ http://site.co.uk/ permanent;
}
When I go to:
http://www.example.com/index.html
http://www.example.com/foo/index.html
..then it correctly sends to:
http://www.example.com/
http://www.example.com/foo/
If I comment those 2 rewrite rules out, restart nginx, then retry... the page loads fine!
Can anyone see where I'm going wrong? Maybe I'm just being blind!
You have constructed a rewrite loop.
The index directive effectively generates an internal rewrite to /index.html whenever a URL with a trailing / is presented.
One way to break the loop is to only apply your rewrite rules when the external URL contains index.html. The variable $request_uri contains the external URL and can be tested using an if directive. See this caution regarding if.
if ($request_uri ~* "/index\.html(?|$)") {
rewrite ^(.*/)index\.html$ $scheme://$server_name$1 permanent;
}
location / {
root /srv/www/site.co.uk/www;
index index.html index.htm index.php;
}

nginx rewrite odoo 8

I am having a few issues trying to rewrite some URLs in nginx.
I have a basic website made using Odoo's CMS. My goal is to make the URLs "pretty"
eg. example.com/services would be what the customer types in and sees in the URL bar but it loads example.com/pages/website.services
The rewrites I have are
location /services {
rewrite / /page/website.services last;
}
location /news {
rewrite / /blog last;
}
location /contact-us {
rewrite / /page/website.contactus last;
}
/contact-us and /news work as intended but /services is still showing example.com/page/services in the URL bar instead of example.com/services
Any help or pointers would be greatly appreciated.
Ok so I managed to solve my problem.
my rewrites that work are:
location /services {
rewrite / /page/services last;
}
location /news {
rewrite / /blog last;
}
location /contact-us {
rewrite / /page/contactus last;
}
I'm not entirely sure why but I think it may have something to do with Odoo doing its own redirects which is why I thought /page/website.services was an actual page.
It confused me somewhat because the /page/website.contactus was working. It may have been because the contactus is created with a module and the services was just created as a page.
If anyone else has a better explanation feel free to post.

Nginx wordpress multi site rewrite rules for subdir install

I am trying to set the rewrite rules in NGINX and I currently have some set for
location / {
already for my main website on that domain.
I would like to insall wordpress in a different folder so my location / {
needs to be location /page {
I can't seem to set the rewrite rules up for this?
Any help would be great I have tried.
Rewrite rules for wordpress 3.0 (multi-site) for nginx?
Doesn't seem to work.
Using this < I can get the page to show up for each user but with no images, css all the other files needed. Its only in plain text.
location /page {
root /home/domain/public_html;
index index.html index.htm index.php;
if (!-e $request_filename ) {
rewrite ^/page/(.*)$ /page/1$ last;
}
}
I need some rules that can be used for the /page location.
You have a small typo:
Rather than using:
rewrite ^/page/(.*)$ /page/1$ last;
Please try:
rewrite ^/page/(.*)$ /page/$1 last;
Regex captures value is $1, not in 1$ ;-)
You will need to change few more things. Here is a complete config - http://rtcamp.com/tutorials/wordpress-nginx-multisite-subdirectories-in-subdirectory-itself/

Please help me understand simple nginx rewrite issue for subdomain robots.txt

For my subdomain I wanted to point to a different robots.txt file. I had hoped the following code would work:
if ($host ~ subdomain) {
rewrite ^/favicon.ico$ /favicon.ico break;
rewrite ^/robots.txt$ /robots.subdomain.txt break;
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2&$query_string last;
}
favicon.ico works fine, all other extensions are rewritten to index.php just fine, but so is robots.txt.
I spent [wasted] a lot of time trying to solve it, which I did by adding the following line after the robots.txt rewrite.
rewrite ^/robots.subdomain.txt$ /robots.subdomain.txt break;
Can someone please help me why it only works when I add this line, also any improvements to my config would be welcomed if you see any obvious inefficiencies! Thank you.
This should be what you're looking for:
location / {
rewrite ^/robots.txt$ /robots.$host.txt; # rewrites robots.txt
try_files $uri $uri/ #php; # try_files serves statics and sends everything else
# to your front controller (index.php)
# files such as favicon.ico get served normally
}
location #php {
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2 last;
}
The only caveat is that your robots.txt needs to be named after the full host, so in the example above your www.domain.com needs to have a robots.www.domain.com.txt file in the document root. This seems a bit ugly, so I'd do it this way instead:
rewrite ^/robots.txt$ /$host.robots.txt;
and then you name your file www.example.com.robots.txt

Resources