To begin with, let me apologise for asking yet another question referring this topic.
I think I have read through all of them in the past few days, and still can not figure out a working solution for my needs.
Essentially I need nginx to redirect:
www.example.com/images/subfolders/.jpeg to images.example.com/subfolders/.jpeg
I currently have this setup:
location /images/ {
rewrite ^/(.*) http://images.example.com$request_uri? permanent;
}
And it kinda works, it redirects to the images.examle.com/images/*.jpeg , but what I need is it to skip the images folder, it will be much cleaner.
As well as that, has anybody seen some site with all of those symbols (^ ~ = + *)in nginx.cnfg explained ?
location /images {
rewrite ^/images(.*)$ http://images.example.com$1 permanent;
}
The explanation of the "^ ~ = + *" symbols can be found in the documentation of the location directive.
Related
I need to enter a bunch or rewrites in my conf file in Nginx. I am not very experienced so I copied what I found before, example.
location = /index.php/blog/blog/xxx/yyy/ {
return 301 /index.php/blog/xxx/yyy/;
}
However I was told that the best way is the following:
location ^~ /index.php/blog/blog/xxx/yyy/ {
rewrite ^/index.php/blog/xxx/yyy/;
}
Which one id the correct one?
The first one is more correct, both location as well as the return -wise, and it'll work faster.
FWIIW, your second snippet looks like it's missing a space in the rewrite after ^, and it's also less efficient, both location as well as rewrite-wise.
References:
http://nginx.org/r/location
http://nginx.org/r/return
http://nginx.org/r/rewrite
I'm trying to do a group capture in a Nginx location block and it's not working for me.
Is what I am trying to do even possible?
location ~* /(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^/$cat/([0-9]+)(.*)$ /page.php?id=$1 last;
}
The error message I am receiving is :
"^/$cat/([0-9]+)(.*)$" does not match "/cars/120/new-car-rentals/"
I have a lot more categories than what I am posting, and trying to prevent writing a rewrite 5x for each specific category name.
Any help would be appreciated.
I'm not familiar with this particular syntax, but based on my experience with others, is it possible that you simply need to escape the forward slashes you're using?
location ~* \/(?<cat>cars|trucks|bikes|motorcycle|quads) {
rewrite ^\/$cat\/([0-9]+)(.*)$ /page.php?id=$1 last;
}
Note the named capture in the location regex: if you want to use a value captured here, you must use the named syntax (?<name>), numbers do not work.
I solved the issue by doing this instead :
location ~* /(cars|trucks|bikes|motorcycle|quad-bikes) {
rewrite ^/([a-zA-Z-]+)/([0-9]+)(.*)$ /page.php?id=$2 last;
...
...
}
The regex ([a-zA-Z-]+) allows me to use characters a-z (case insensitive) with possible dashes in my category / page names.
I have categories I'd like to rewrite.
for example:
example.com/videos?c=18
to:
example.com/category/name
I tried to do this using multiple examples, most had no effect and this example gave me only page not found to all /videos pages:
location /videos {
if ($args ~ "c=18") {
rewrite ^/videos(.*) http://$server_name/category/name$1 permanent;
}
}
Is this even doable purely via Nginx what I am trying to achieve?
As far as I got the question, currently your site has this url scheme, which you can't change:
http://example.com/videos?c=18
But you would like to present visitors with "pretty"-looking URLs like
http://example.com/category/name
That pretty URL does not really exist anywhere on the site, which is why you have to rewrite it, e.g. turn pretty virtual url into a real one that your scripts can process.
Once again, you rewrite from virtual to actual, not the other way round.
The following directive would turn /category/cars/ into /videos?c=cars
location /category {
rewrite ^/category/(.*)$ /videos?c=$1 last;
}
But your script won't understand /videos?c=cars url, it needs category ID to work. So in your case the pretty url should look like
http://example.com/category/18
which will be rewritten to
http://example.com/videos?c=18
"if" directive is not best solution, but in your case you can try "if" and $arg_name (argument name in the request line):
location /videos {
if ($arg_c = "18") {
rewrite ^/videos(.*) http://$server_name/category/cars? permanent;
}
if ($arg_c = "19") {
rewrite ^/videos(.*) http://$server_name/category/bikes? permanent;
}
# and so on
}
We have several SEO pages like:
http://www.example.com/PageOne.html
Which we redirect in config like:
location = /PageOne.html {
rewrite ^/(.*) /seo.php?id=1 last;
}
Problem is if a user access this page by typing:
http://www.example.com/pageone.html
"Page Not Found" error is displaying. There are approximate 500+ seo pages. How to write rule for nginx to ignore case sensitivity in url? I want a common solution for all url.
Specifically for PageOne.html, you can do the following:
location ~ /PageOne.html {
return 301 http://www.example.com/pageone.html$1;
}
If you have multiple URIs which need to be redirected, it appears the best option is to use Perl:
location ~ [A-Z] {
perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}
If you have hundreds of unique URIs which would involve many location blocks as above, I'd consider changing your application to handle lowercase URIs rather than expecting the webserver to handle the lowercase conversion.
This solved my issue. Sad to say that there is not many articles related to these issues, even nginx doesn't provide user friendly Help/Tutorials.
location ~* ^/-PageOne.html {
rewrite ^ /seo.php?page_id=1 last;
}
Hope this helps!
The original url is https://www.mywebsite.com/women/shoes_1-+-1~2.html
The redirected url should be https://www.mywebsite.com/women/shoes.html
rewrite ^/women/shoes_1-+-1~2\.html /women/shoes.html permanent;
I am sure the answer is simple but I don`t see it!
Thank you for your help.
The problem was that I did not put "\" before the "-" and "+".
The final version that I used is:
rewrite ^/women/shoes_1\-\+\-1~2\.html /women/shoes.html permanent;
I would do it like this:
location ~ ^/women/shoes_1\-\+1~2\.html$ {
rewrite ^(.*)$ /women/shoes.html permanent;
}
For the best performance, I would try:
location = /women/shoes_1-+-1~2.html {
return 301 https://www.mywebsite.com/women/shoes.html;
}
Note that rewrite directives and regex locations are dependent on their position in your config. By avoiding them, your config will scale more smoothly.