nginx rewrite odoo 8 - nginx

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.

Related

Write all nginx redirection of the website pages in single location block

I want to write all the redirections of my website pages in one location block
This is what I have done so far
location /mywebsite {
rewrite ^/mywebsite$ /index.php;
rewrite ^/mywebsite/about$ /about.php;
rewrite ^/mywebsite/services$ /services.php;
rewrite ^/mywebsite/events$ /events.php;
rewrite ^/mywebsite/events/(.*)$ /events.php?id=1 last;
}
but this does not work as intended.

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 redirect setting

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.

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