nginx not rewriting url for opencart - nginx

I have an opencart setup and I'm trying to get nginx to rewrite a url, yet I must be doing something wrong, as nothing I try will work. Here is what I have now that is not working. Just want to remove the index.php?route=common/home from the URL
server {
...
location /index.php?route=common/home {
rewrite http://www.site.com/ permanent;
}
}

How about this
server {
rewrite ^/index.php?route=common/home$ / permanent;
}

Related

How can I make a nginx.conf file to return different web pages for different subdomains?

I want to make my nginx to show a web page for this subdomain: abc.xyz.com and a different web page for this subdomain: abcd.xyz.com. How can I make that?
I tried to change the location block but it didn't work. Please help me.
You should list both domains in server_name directive:
server {
server_name abcd.xyz.com abc.xyz.com;
location /page {
}
location /anothe_page {
}
}
Or create two different servers as follow:
server {
server_name abc.xyz.com;
location /page {
}
}
server {
server_name abcd.xyz.com;
location /anothe_page {
}
}

Redirect search page to search result page using nginx

I want to redirect my page from
https://www.example.com/search?q=testtest or
https://www.example.com/search/?q=testtest
To
https://www.example.com/search-results/?q=testtest
So I have added below the code in nginx. But redirect not working.
location = /search {
if ($args ~ "q=([^/]*)/?$"){
rewrite ^/search-results/$ /?q=%1 redirect;
}
}

How to redirect a few urls in nginx?

I have some requests from users to make some pages indiscoverable in search engines. While it would be an idea to use robots.txt, I would like to redirect such pages to a page explaining what happened in this case, using NGINX.
In essence, this is what I want:
http://example.com/some/url.pdf --> http://example.com/redirected_due_to_privacy
http://example.com/another_url --> http://example.com/redirected_due_to_privacy
http://example.com/and/another --> http://example.com/redirected_due_to_privacy
I want all the other URLs to point itself (remain the same).
What is the best way to do this?
I think you can create different urls for each pages, and add the rewrite rules in location module of nginx.conf.
Such as:
rewrite "^/redirected_due_to_privacy$" /some/url.pdf break;
rewrite "^/redirected_due_to_privacy1$" /another_url;
rewrite "^/redirected_due_to_privacy2$" /and/another
map, rather than rewrite, return or redirect, is useful in this case:
map $uri $privacyredir {
default 0;
/some/url.pdf 1;
/another_url 1;
/and/another 1;
}
...
server {
...
# putting this inside a location is be more efficient
if ($privacyredir) {
return 301 $scheme://$host/redirected_due_to_privacy;
}
}
(Solution suggested by #vandemar)

Nginx Rewrite rule not working with subdomain

I want to redirect URLs like this:
http://example.com/tmcontent/comprehension/el_semenario.mp3
to this:
http://static.example.com/tmcontent/comprehension/el_semenario.mp3
Currently I am doing this
rewrite /(tmcontent.*)$ http://static.example.com/$1 redirect;
But sadly doesn't work.
location /tmcontent {
return 301 http://static.example.com/$request_uri;
}

Why my nginx config for hiding jsp extension doesn't work properly?

I configure my Nginx web server with follow setting, It hides .jsp extension but doesn't appear my page.
location ~ \.(jsp)$ {
proxy_pass http://192.168.2.225:9595;
if ($request_filename ~* ^(.*).jsp$)
{
rewrite ^/(.*).jsp$ /$1 redirect;
break;
}
}
location ~ \.jsp {
return 403;
}
That will make *.jsp inaccessible. If you want them accessible but only via internal rewrites, then change the 'return 403;' to 'internal;'.

Resources