Find multiple strings between two arguments - go

I'm trying to make a function that will get a string between two arguments (rewrite and redirect). I can't wrap my head around it.
I have a string that looks like this:
add_header X-Robots-Tag "noindex, follow" always; rewrite ^/en/about/Administration/index.aspx /en/about/more-about/administration redirect; rewrite ^/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration redirect; rewrite ^/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields redirect; rewrite ^/en/about/For-companies/index.aspx /en/about/more-about/for-companies redirect; rewrite ^/en/about/contact-information/index.aspx /en/about/more-about/contact-information redirect; rewrite ^/en/about/index.aspx /nl/over redirect;
And I want the following output:
/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over
What is the proper regex or method to get all strings between the two arguments?

Try:
\brewrite \^(.*?)\s+redirect;
See an online demo
\brewrite \^ - literally 'rewrite' between a word-boundary on the left and a space followed by literal '^' on the right;
(.*?) - Match (lazy) 0+ characters;
\s+redirect; - Literally 'redirect' between 1+ whitespace characters on the left and a semi-colon on the right.
See an online GO demo which will print:
/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over

Related

Wildcard Prefix Laravel 5

is there a way in Laravel 5 to have a recognized list of prefixes, such as ['gbr','en','eu'], so that
/gbr/bar/baz/shoelace // or
/eu/bar/baz/shoelace
is handled by the same controller#method as
/bar/baz/shoelace
Except that the additional parameter foo=gbr is passed in the first condition?
Note the Route::group prefix won't work because there may or may not be a prefix in this case. Also, this strategy should take precedence over all else, i.e. the Route would check for the (optional) prefix first.
Yes there is a way.
When declaring your routes, you can declare them as
Route::get('{prefix}/bar/baz/shoelace', 'controller#method')->where('prefix', 'gbr|en|eu');
gbr|en|eu is a simple regular expression that will match either the string gbr, en or eu. Check out Regular expression constraints for more details
And in your controller you can have
public function method($prefix) {
//code here
}

Nginx bypass cache on specific RESTful endpoints

I have the following type of endpoints.
/v1/endpoint/{id}/cache
/v1/endpoint/{id}/bypasscache
I want to bypass the cache on the second one as it needs to be up to date, whilst keeping my default cache on the first url which is updated infrequently.
Is this possible to have this setup, but with variable id?
if ($request_uri ~* "/v1/endpoint/18/bypasscache" ) {
set $no_cache 1;
}
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
Try (for variable ids in 1 or 2 character wide combinations of numbers, lower and upper case ascii letters):
if ($request_uri ~* "/v1/endpoint/[0-9a-zA-Z]{1,2}/bypasscache" ) {
set $no_cache 1;
}
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
untested as I currently cannot start a spare web service ;-)
Other hints may be found in answers to nginx location regex - character class and range of matches
Also there is the hint on not forgetting to quote the string when it contains curly braces in the regex, as these might interfere with the block syntax curly braces - here the block of the if ...

Ruby regex doesn't recognize beginning of string

I want to replace the "text" contains http link with the actual HTML markup for this link.
Here is my Ruby code
url_check = Regexp.new( '(\A|[\n ])([\w]+?://[\w]+[^ \"\r\n\t<]*)', Regexp::MULTILINE | Regexp::IGNORECASE )
self.gsub!(url_check, '\1\2')
to_s
Here is a test case:
This is entrance page for the service (using HTML):
http://foobar.org/resources?format=html
Let us pick the "contributions" namespace: http://foobar.org/
The link is created only for the second case, but not for the first (which has several line breaks before)
I suggest using \b (word boundary) instead of new-line/start-of-the-line detection:
.gsub!(/\b([\w]+?:\/\/[\w]+[^ \"\r\n\t<]*)/i, '\1')
you don't need "http:" in replacement as you already match for protocol.

nginx rewrite URL

I want to rewrite the following URL:
index.php?SOMETHING=VALUE
As
/SOMETHING/VALUE
I'm inexperienced with nginx rewrites so any help would be appreciated.
Thanks
I've come up with a solution to your problem :
location /index.php {
if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) {
rewrite ^ /${PATH1}/${PATH2}? last;
}
}
Explanations:
if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) : captures the two relevant sections from the URL parameter, storing the values in variables PATH1 and PATH2
rewrite ^ means "rewrite the entire URI"
/${PATH1}/${PATH2} is constructing the new URI
the trailing ? informs nginx that you don't want to append the original URL parameters
last tells nginx to continue to follow rules after the rewrite

nginx rewrite redirect rule does not work properly

I want to redirect urls like http://mysite.com/?action=add&goback=1 to http://myiste.com/add/?goback=1.
I came to:
location / {
if ($request_uri ~* /\?action.*) {
add_header Cache-Control private;
rewrite ^/\?action=([^&]*)&?(.*)$ /$1/?$2 permanent;
}
}
but it does not work and I don't see why.
P.S. I needed add_header just to indicate if ($request_uri ~* /\?action.*) { took place.
I've tried several other solutions like
rewrite ^/\?action=(\w+).*$ /$1/?$query_string permanent;
no success.
UPDATE:
The actual goal is:
if URL looks like this
http://mysite.com/add/?param1=param1&...
/(add)/ should become an action param:
location ~ /[\-\w]+/ {
rewrite ^/([^/]*).*$ /?action=$1 last;
...
}
this seems to be working properly and I guess I don't need extra-help with this one
if URL looks like this:
http://mysite.com/?action=add&param1=param1&...
nginx should redirect to:
http://mysite.com/add/?param1=param1&...
When I try to do this part I'm getting results I don't need - infinite redirect loops, URLs like http://mysite.com/add/?action=add&param1=param1&..., etc.
Is there the solution addressing my goal?
This is because rewrite only matches URI path, without query sting aka arguments, much like location. Try something like this instead:
location = / {
if ($arg_action) {
rewrite ^ /$arg_action/ permanent;
}
}
The location = / check will make sure the rule is only applied to requests to /, if ($arg_action) checks if there is action=... argument, and rewrite ^ /$arg_action/ permanent; will actually do a redirect to rewritten URI. Query string will be preserved (as by default) from original request.
See http://nginx.org/r/rewrite for docs.

Resources