I am geting 404 on different urls that end with the same string and instead of creating multiple redirects I would like to catch them all on the last string. It always appears at the same position, pattern goes like so:
/some-of-my-urls/the-same-string
No trailing slash there. I tried something like this:
url(r'^[a-zA-Z0-9_]+/the-same-string', redirect_func),
url(r'^./the-same-string', redirect_func),
But that doesn't work. Probably obvious for somebody with more regex knowledge, I am not very advanced. Anybody ideas?
You may use a negated character class [^/] to match any char but / and quantify it with a + quantifier that matches 1 or more repetitions:
r'^[^/]+/the-same-string'
See the regex demo.
Related
this is a regex of a proxy, if I add this to my proxy:
(.*\.|)(abc|google)\.(org|net)
my proxy will not transmit the abc.org, abc.net, google.org, google.net's traffic.
how can I write a regex opposite to this regex? I mean only transmit the abc.org, abc.net, google.org, google.net's traffic.
EDIT-01
My thought is just want to transmit abc.org or www.abc.org, how can I do with that?
Try this:
^(?!(www\.)?(?:abc|google)\.(?:net|org)).*
Demo: https://regex101.com/r/WOnFx8/3/
I used ?! to reverse the matching of your regex. This way, it will match any domain except these specific 4 domains.
Another way to do it is by using this code to include anything before the desired domains:
^(?!(.*\.|)(?:abc|google)\.(?:net|org)).*
demo: https://regex101.com/r/WOnFx8/4/
Your regex you write
(.*\.|)(abc|google)\.(org|net)
mean any string is one of abc.org, gooogle.org, abc.net, google.net, with optional prefix string ends with dot (.)
Like: test.google.org, sub.abc.net,...
I think you want to match string like test.yahoo.com, but not test.google.org. If you can use negative look ahead, this is the answer:
^(.*\.|)(?!(abc|google)\.(org|net))\w+\.\w+$
Explain:
^ and $ to be sure your match is entire url string
Negative look ahead is to check the url is not something like abc.org, abc.net, google.org, google.net
And \w+\.\w+ to check the remain string is kind of URL type (something likes yahoo.com, etc...)
Im going to assume you have lookaheads, if so then you can simply use -
(^.*?\.(?!(abc|google))\w+\.(?:org|net)$)
Demo - https://regex101.com/r/5eC41R/3
What this does is -
Looks for the start of the url (till the first .)
Checks that next part is not abc or google
looks for the next section (till the next .)
Looks for a closing org or net
Note that since it is a lookahead it will be slow compared to other regex matches
I need a regex for Url rewrite module, to validate urls in such way:
1) spa/ - match
2) spa/some/url - match
3) spa/some-url - match
4) spa/some.js - no match
5) spa/some.css - no match
So, it should match, if url
a) starts with "spa"
b) ends with whatever except ".js" or ".css"
What I tried to test is ^(spa/)((?!.js)|(?!.css))$
but it's not working.
Thank you and sorry if it's duplicated.
Try this regex:
^spa\/((.+)\/)*.*(?<!\.js|\.css)$
with g and m flags set.
Please note that this regex allows several characters that urls are not supposed to have. I have tried to keep it simple. So, you might want to tune it a bit before using it.
You need negative-lookbehind for this.
Try this (you may need to modify it slightly)
^spa.*(?<!(\.js|\.css))$
^spa : string beginning with spa
.* : followed by any character(s)
(?<!(\.js|\.css))$ : not ending with .js or .css
I'd like to point all of my visitors to "single subdirectories" to one page, and all visitors to "double subdirectories" to another. E.g:
/foo/
/new/
/north/
/1-j4/
Would all point to 1.app, whereas
/foo/bar/
/new/york/
/north/west/
/1-j4/a_990/
Would all point to 2.app.
I figured I could do this with non-greedy regex matching, like so:
- url: /(.*?)/$
script: 1.app
- url: /(.*?)/(.*?)/$
script: 2.app
To my confusion, both /foo/ and /foo/bar/ resolve to script 1.app. Does the "lazy" regex force itself up to include the middle /, since that's the only way to get a match? How else can I do this? I have tried using (\w*?) but get the same result.
The .*? will still match through any amount of / because . matches any character but a line break char (by default). You need to base your regexps on a negated character class, [^/]*, that matches 0 or more chars other than /.
To match directories with one part, use ^([^/]*)/?$ and to match those with 2, use ^([^/]*)/([^/]*)/?$.
Note that if you plan to use the patterns in online Web testers, you will have to escape / in most of them as by default they use / symbol as a regex delimiter.
Yes, the (.*?) includes slashes, so will resolve to 1.app. If you put the 2.app handler first, it should do what you want:
- url: /(.*?)/(.*?)/$
script: 2.app
- url: /(.*?)/$
script: 1.app
I'm trying to apply a route filter to all the routes except homepage. I do it like this:
Route::whenRegex('/^\/[\S]+/', 'myFilter');
So, basically, I'm saying: match all the routes starting with /, followed by any non-whitespace character(s). However, the filter doesn't work.
The filter itself:
Route::filter('myFilter', function() {
if (Session::has('userRegState')) {
return Redirect::action('DefaultController#home');
}
});
I checked it - the userRegState session variable is set, but no redirect is done. Is the regex used in the filter wrong?
OK, it seems there might be some kind of forward slash stripping done in the routing logic of Laravel, which makes my pattern not match what it should. To fix it, I omit the forward slash.
Instead of this:
Route::whenRegex('/^\/[\S]+/', 'myFilter');
I do that:
Route::whenRegex('/^\S{2}/', 'myFilter');
The final regex matches anything that starts with at least 2 non-whitespace characters. In my case it's forward slash followed by any non-whitespace character, like /a, /page2, etc.
I am writing some mod_rewrite regex, and I have several request URLs that lookg like that
use1mycompany
use2mycompany
use3mycompany
use4mycompany
but also I have and some request URIs that start with
mycompany/user1mycompany/
mycompany/user2mycompany/
mycompany/user3mycompany/
mycompany/user4mycompany/
In order to fix that Issue I have used the following regex in apachec mod_rewrite
^(.*)mycompany/?$
the problem is that my regex matcing both the userXmycomany URL and the mycopmany/userXmycompany/
So, the question is, how can I match the urls that not starts with the string "mycompany" but end with the string "mycompany" ?
Kind regards
(?<!mycompany)/.*(?<=mycompany)/?
will match /user1mycompany/ but not mycompany//user1mycompany/
You should match string which have at least one character before mycompany.
* means 0 or more. + means 1 or more.
^(.+)mycompany/?$