There are a lot of bad request of images and files on my site... but with names close from the real name. basically, if i have a file called example.gif, there is a call to example.gifzerzer. What i want is to rewrite to keep the name and the extension, but nothing after it.
I did create a map file with rewrite (it's working for example the tmp.php to index.php rewrite below except the rule for removing everything after extension).
Here is what i've done so far :
/(.+\.(bmp|gif|ico|jpeg|jpg|png|svg)).+ /$1;
this is not working neither
/(.+\.(bmp|gif|ico|jpeg|jpg|png|svg)).+? /$1;
my map :
map $request_uri $redirect_uri {
default "";
/(.+\.(bmp|gif|ico|jpeg|jpg|png|svg)).+ /$1;
/tmp.php /index.php;
}
i was under Apache before migrating to Nginx and this was working :
RewriteRule ^([a-z0-9\-/_]*\.(bmp|gif|ico|jpeg|jpg|png|svg)).+ /$1 [R=301,L]
I did that rewrite that is working, when put in the server section, but then it is not included in the map.
location ~ (.+\.(bmp|gif|ico|jpeg|jpg|png|svg)).+$ {
return 301 /$1;
}
if someone has an idea why, or can confirm that i'm doing it right ?
Related
My site works correctly as
https://example.com/website/page/Home
https://example.com/website/page/AboutUs
I would like to remove the /website/page part so that end users would see https://example.com/Home etc.
I read on Nginx website that rewrites are not preferred so I tried:
Location / {
try_files $uri /website/page$uri;
}
I get an internal serval error. What is the right way?
Consider using a rewrite like:
# rewrite example.com/awesome to example.com/website/page/awesome
rewrite ^(/.*) /website/page$1 break;
This should work similarly:
rewrite ^/(.*) /website/page/$1 break;
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;
I want to rewrite all my JPG file URLs using mobify CDN. For that, all I have to do is prepend the URL
https://ir0.mobify.com/jpg50/ to my existing URL. So for example, if I have the URL
http://xxx.yyy.com/wp-content/uploads/2290/07/abc.png then the user has to be redirected to
https://ir0.mobify.com/jpg50/http://xxx.yyy.com/wp-content/uploads/2290/07/abc.jpg
I wrote the following code in my nginx config. I tested the regexs at regexlib and they seem to be fine.Still do not understand what is wrong with my config. Please help.
location ~ \.jpg$
{
rewrite ^http://(.*).jpg$ https://ir0.mobify.com/jpg50/$uri last;
}
Try this ...
location ~ \.jpg$ {
return 301 https://ir0.mobify.com/jpg50$request_uri;
}
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.
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