rewrite ^/#(.*)$ /user/$1/ last;
I want to rewrite foo.com/#bar to foo.com/user/bar.
Not an Nginx expert, but here is what you need to do
rewrite /#([^\W]+) /user/$1/ last;
to prove that, I have written a small Javascript code that demonstrates the replace part.
const url = "foo.com/#bar";
const result = url.replace(/#([^\W]+)/, "user/$1");
console.log(`the result is ${result}`);
Result
the result is foo.com/user/bar
Related
Those are my two nginx rules :
rewrite ^(/v1/foobar)(.*)$ /final$2 permanent;
rewrite ^(/v1/foobar-tow)(.*)$ /final$2 permanent;
I expect that /v1/foobar-two/foo will use the second rule and then will redirect to /final/foo but I doesn't : since the URL starts with foobar, the first rule will be used and finally will redirect to final-two/foo : the concatenation of the first target and the difference between the first and second rule !
To solve the problem I've just inversed the rules, but I wonder if there is a better solution !
To sum up, this is what I'm looking for :
/v1/foobar -> /final
/v1/foobar/hello -> /final/hello
/v1/foobar-tow -> /final
/v1/foobar-tow/hola -> /final/hola
To avoid surprises with NGINX, and increase configuration scalability, you should, generally:
try to live without rewrite directive; as per NGINX author it simply appeared before location and in many cases you can now use location with capture groups in lieu of rewrite
design your regular expressions in the way that the order of matching is not important, if possible
use exact matching, where applicable
isolate regular expression locations under a prefixed one
Putting all things together:
location /v1/ {
location = /v1/foobar {
return 301 /final;
}
location ~ ^/v1/foobar/(\w+)$ {
return 301 /final/$1;
}
location = /v1/foobar-tow {
return 301 /final;
}
location ~ ^/v1/foobar-tow/(\w+)$ {
return 301 /final/$1;
}
}
I have problem with specific redirect
My url is
http://example.com/test/some_file.jpg?refferer=mobile
And this must redirect to
http://example.com/parser.php?q=some_file.jpg
Normal redirect(without get params) i i've done by
rewrite test/(.*) /parser.php?q=$1 last;
location ~ ^/test/(.*)$ {
if ($arg_referrer = mobile) {
return 301 http://example.com/parser.php?q=$1;
}
# else ...
}
http://nginx.org/r/rewrite
If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended.
So answer is really simple:
rewrite test/(.*) /parser.php?q=$1? last;
I'm having trouble fine tuning a regex for Nginx url rewrite rules, What I am trying to do is take the first two pieces of the url and convert them to variables (nothing too fancy, and should be simple).
e.g. I type in http://www.webserver.com/piece1/piece2 and get http://www.webserver.com/rewtest.php??val1=piece1&val2=piece2
So far I have:
location / {
rewrite ^/(.*)/(.*)/? /rewtest.php?val1=$1&val2=$2 last;
return 404;}
}
which does seem to work. The problem is if the user types http://www.webserver.com/piece1/piece2/ it gives val 1 as piece1/piece2 (as 1 variable, not 2).
Also if the user were to type http://www.webserver.com/piece1/ I currently get piece1 in var 1, which is great. BUT if the user types http://www.webserver.com/piece1 it gives me an error and I'd like to get the same (var 1=piece1).
Any help greatly appreciated as I am new to regexs!
Seems as if / is also recognized by "."...try:
location / {
rewrite ^/([^/]+)/([^/]+)/? /rewtest.php?val1=$1&val2=$2 last;
return 404;}
}
Preset:
got a gallery script that is creating thumbnails like so:
GET /path/to/script/thumb.php?src=host.tld/path/to/file.jpg
Goal:
rewrite the hostname in src to a different name:
GET /path/to/script/thumb.php?src=newhost.tld/path/to/file.jpg
It's only the host that I need replaced, everything else should stay the same.
I've tried a couple of things but nothing produces the result that I want.
This is the code as it is now (not working):
location ~* thumb {
if ($args ~ "(src=http://host.tld:8080/)(.*)" ) {
set $params $2;
rewrite ^.*$ thumb.php?src=http://newhost.tld/$params? last;
}
}
also, host.tld could be just about anything so matching on host.tld literally is just a simplification for now.
I have a URL validation method which works pretty well except that this url passes: "http://". I would like to ensure that the user has entered a complete url like: "http://www.stackoverflow.com".
Here is the pattern I'm currently using:
"^(https?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+#)?" //user#
+ #"(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP- 199.194.52.184
+ "|" // allows either IP or domain
+ #"([0-9a-z_!~*'()-]+\.)*" // tertiary domain(s)- www.
+ #"([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // second level domain
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // port number- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:#&=+$,%#-]+)+/?)$"
Any help to change the above to ensure that the user enters a complete and valid url would be greatly appreciated.
Why not use a urlparsing library? Let me list out some preexisting url parsing libraries for languages:
Python: http://docs.python.org/library/urlparse.html
Perl: http://search.cpan.org/dist/URI/URI/Split.pm
Ruby: http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/uri/rdoc/classes/URI.html#M001444
PHP: http://php.net/manual/en/function.parse-url.php
Java: http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html#URI(java.lang.String)
C#: http://msdn.microsoft.com/en-us/library/system.uri.aspx
Ask if I'm missing a language.
This way, you could first parse the uri, then check to make sure that it passes your own verification rules. Here's an example in Python:
url = urlparse.urlparse(user_url)
if not (url.scheme and url.path):
raise ValueError("User did not enter a correct url!")
Since you said you were using C# on asp.net, here's an example (sorry, my c# knowledge is limited):
user_url = "http://myUrl/foo/bar";
Uri uri = new Uri(user_url);
if (uri.Scheme == Uri.UriSchemeHttp && Uri.IsWellFormedUriString(user_url, UriKind.RelativeOrAbsolute)) {
Console.WriteLine("I have a valid URL!");
}
This is pretty much a FAQ. You could simply try a search with [regex] +validate +url or just look at this answer: What is the best regular expression to check if a string is a valid URL
I use this regex. It works fine for me.
/((([A-Za-z]{3,9}:(?://)?)(?:[-;:&=+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+\$,\w]+#)[A-Za-z0-9.-]+)((?:/[+~%/.\w-]*)?\??(?:[-+=&;%#.\w])#?(?:[\w]))?)/