I'm trying to manually redirect a couple of links from my old blog to my new blog like this:
location = /blog-article-url {
return 301 https://blog.example.com/blog-article-url
}
And this works when i visit https://www.example.com/blog-article-url, i get properly redirected. However it IS case sensitive, if i visit https://www.example.com/BLOG-ARTICLE-URL it will NOT work.
What should i replace the = sign in the nginx config block to make it case insensitive?
You can do a case insensitive location block with regular expressions.
For example:
location ~* ^/blog-article-url$ { ... }
Note that the evaluation order of regular expression locations is significant - so you may need to move this location block towards the top of your server block. See this document for more.
Related
I want to block a specific URL but I am not able to do this.
The URL that should be blocked is example.com/clientarea/?dxx_g=dddd.
But the following url should still work - example.com/clientarea.
I tried the following:
location ^~ /clientarea/ {
return 444;
}
But if I do this it will block all connections to /clientarea.
I hope you can help me or advise me how to make this possible.
The location and rewrite statements test a normalized URI which does not include the ? and anything following it.
The $request_uri variable contains the entire URI. Test this variable using an if or map directive.
For example:
if ($request_uri = /clientarea/?dxx_g=dddd) {
return 444;
}
You can also use regular expressions. See this document for more. See this caution on the use of if.
If you have a number of URIs to block, you should consider using a map instead.
I mapped URL in Nginx ( Bulk Level of Redirect)
nginx.conf
map $uri $new_url2{
include /var/www/html/**ext.map**;
}
=== **ext.map** Inside File
/en_us/abc.html /en_us/abc22.html;
/it_it/abc.html /it_it/abc22.html;
/fr_fr/abc.html /fr_fr/abc22.html;
nginx_host.conf
if ($new_url2)
{
return 301 $new_url2;
break;
}
It's working fine. Because of location block in URL, I need to generate multiple times for individual country wise.
I am trying to capture the location section and add with redirect URL.
You can use a regular expression in the map block by introducing it with a ~ or ~*. See this document for details.
Use a named capture to record the language code so that it can be used to construct the new URI. Use double quotes around an expression that contains braces {}.
For example:
"~*^(?<lang>/\w{2}_\w{2})/abc.html" $lang/abc22.html;
I have a rule which is not working correctly.
I need it so that whenever URL xxx.com/forum/css.php is hit, it is re-written to xxx.com/forum/core/css.php.
I have written the following location block for it:
location ~^ /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
Also needing to be taken into account is that the file is a factory so it accepts parameters, the url being hit actually looks like xxx.com/forum/css.php?x=123&y=string. Will this also be taken into account in the re-writes or does it need to be specified? Sorry if the question seems silly I am just beginning to work with servers! Thanks fellow coders!
To rewrite a single URI (with or without query string) you could use a location =:
location = /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
The rewrite directive appends the query string (unless terminated with ?). See this and this for more.
I.e., what is the difference between
location somefolder {
}
and
location somefolder/ {
}
and
location /somefolder {
}
and
location /somefolder/ {
}
I know it's kind of a silly question but honestly sometimes I get myself confused, a concise answer would be nice!
Found my own answer.
The first two configurations will not match anything, because every location starts with a "/". So they are basically invalid locations. A commenter on Server Overflow mentioned this and it proved to be correct.
The second two, for most purposes, are equivalent, in that NGINX will 301 redirect the third to the fourth. So a request for http://somedomain/somefolder will be redirected to http://somedomain/somefolder/. If you really have a location called /somefolder, such as a file literally called that, then you could create a location with the "=" operator, which forces an exact match, so
location = /somefolder {
}
For best practice just always use the /somefolder/ format unless you really need the exception I just mentioned.
I'm trying to set up a permanent redirect from
http://domain.com/member/blog_post_view.php?postId=1
to
http://blog.domain.com/friendly-url-here
The source URL contains both a ? and an = which I think might be the cause but am unsure.
I've tried all sorts of nginx suggestiosn including the one below but can't seem to get the redirection to work and hoped someone can point me in the right direction.
location /blog_post_view.php?postId=1 {
rewrite "/blog_post_view.php\?postId\=1" http://blog.domain.com/friendly-url-here permanent;
}
That part of request line starting from the question mark is called query string, while the location directive matches only path part of URI.
You should use $arg_* variable instead:
location =/blog_post_view.php {
if ($arg_postId = 1) {
return 301 http://blog.domain.com/friendly-url-here;
}
}
Reference:
http://nginx.org/r/location
http://nginx.org/r/if
Embedded Variables