Trying to write a rule for a WP redirect
I've got the rule ^((?!json).)*$
Which ignores:
/wp-json/aber
And the rule ^([^.\?]*[^\/])$
Which ignores:
/aber.css
/france?foobar=1
But I can't seem to figure out how t combine them into a single rule that ignores all three. if I add [^.?|json] if rules out any URLs including any of those characters individually. It only needs to be exactly "json" located anywhere in the URL or URLs that include a . or a ?
Examples:
Dont match:
/wp-json/aberdeen
/aberdeen.css
/aberdeen?foobar=1
Match:
/wp-jsn/aberdeen
/aberdeen
Well the first pattern ^((?!json).)*$ simply asserts that json does not appear anywhere in the input. We can refactor the second pattern by adding a negative lookahead to rule out json:
^(?!.*json)([^.\?]*[^\/])$
Related
I know there are many similar out there but I did not find a working solution for me.
I have incoming URLs like: https://example.com/123-frank-street
and want them to rewrite to: https://example.com/street/index.php?name=123-frank-street
I tried dozens of versions and my closest is the following
RewriteRule ^[0-9]+([A-Za-z0-9-]+)/?$ /street/index_test.php?street=$1 [NC,L]
This only rewrites to: https://example.com/street/index.php?name=-frank-street
The 123 is missing and gets somehow not forwarded. only the rest!
What I am missing here or doing wrong?
thx in advance
Rewrite rules are really quite simple once you understand their structure:
On the left is a regular expression which determines which URLs from the browser the rule should match
Inside that regular expression, you can "capture" parts of the URL with parentheses ()
Next, is the URL you want to serve instead; it can include parts that you captured, using numbered placeholders $1, $2, etc. It's entirely up to you where you put these, and Apache won't guess
Finally, there are flags which act as options; in your example, you're using "NC" for "Not Case-sensitive", and "L" for "Last rule, stop here if this matches"
In your example, the pattern you are matching is ^[0-9]+[A-Za-z0-9-]+/?$, which is "from start of URL, 1 or more digits, one or more letters/digits/hyphens, 0 or 1 trailing slash, end of URL".
The only part you're capturing is ([A-Za-z0-9-]+), the "one or more letters/digits/hyphens" part; so that is being put into $1. So the rest of the URL is being "discarded" simply because you haven't told Apache you want to put it anywhere.
If you want to capture other parts, just move the parentheses, or add more. For instance, if you write ^([0-9]+)([A-Za-z0-9-]+)/?$ then $1 will contain the "one or more digits" part, and $2 will contain the "one or more letters/digits/hyphens" part.
I always have I kinda rough time working with regexes. I'm trying to make a regex that matches routes, when the route has parameters set:
For instance:
/post/1 matches /post/{id}
/post/5/ doesn't match /post/{id}
/post/6/comments/4 matches /post/{id}/comments/{comment}
/post/a-random-slug matches /post/{id} or /post/{slug} (whatever you want to name the param)
/user matches /user, but not /user/
What I currently did is create a regex for every route, and then match the current URI against that route regex.
What I currently have is:
My regex
In this example I try to make a regex for the route: /post/{param1}/{param2}. Meaning it should match /post/ then a parameter and another parameter, but nothing after that parameter.
As you can see: ^(\/post\b)(\/.{1,}\/)(.{1,}\b)$ matches /post/what-is-your-name/5, and when I add another / it doesnt match anymore. However if you add characters after that regex again it starts to match again.
Meaning that:
/post/what-is-your-name/5/ doesn't match
/post/what-is-your-name/5/more does match
Does anyone have an idea how I can accomplish the first example?
I'm by far someone who knows a lot about regexes, if someone sees a better way to match URIs against routes then please let me know.
Hope this will help you out
Regex: ^(?:\/post\b)(?:\/[\w]+){2}$
Regex demo
I'm trying to write a regexp.
some background info: I am try to see if the REQUEST_URI of my website's URL contains another URL. like these:
http://mywebsite.com/google.com/search=xyz
However, the url wont always contain the 'http' or the 'www'. so the pattern should also match strings like:
http://mywebsite.com/yahoo.org/search=xyz
http://mywebsite.com/www.yahoo.org/search=xyz
http://mywebsite.com/msn.co.uk'
http://mywebsite.com/http://msn.co.uk'
there are a bunch of regexps out there to match urls but none I have found do an optional match on the http and www.
i'm wondering if the pattern to match could be something like:
^([a-z]).(com|ca|org|etc)(.)
I thought maybe another option was to perhaps just match any string that had a dot (.) in it. (as the other REQUEST_URI's in my application typically won't contain dots)
Does this make sense to anyone?
I'd really appreciate some help with this its been blocking my project for weeks.
Thanks you very much
-Tim
I suggest using a simple approach, essentially building on what you said, just anything with a dot in it, but working with the forward slashes too. To capture everything and not miss unusual URLs. So something like:
^((?:https?:\/\/)?[^./]+(?:\.[^./]+)+(?:\/.*)?)$
It reads as:
optional http:// or https://
non-dot-or-forward-slash characters
one or more sets of a dot followed by non-dot-or-forward-slash characters
optional forward slash and anything after it
Capturing the whole thing to the first grouping.
It would match, for example:
nic.uk
nic.uk/
http://nic.uk
http://nic.uk/
https://example.com/test/?a=bcd
Verifying they are valid URLs is another story! It would also match:
index.php
It would not match:
directory/index.php
The minimal match is basically something.something, with no forward slash in it, unless it comes at least one character past the dot. So just be sure not to use that format for anything else.
To match an optional part, you use a question mark ?, see Optional Items.
For example to match an optional www., capture the domain and the search term, the regular expression could be
(www\.)?(.+?)/search=(.+)
Although, the question mark in .+? is a non-greedy quantifier, see http://www.regular-expressions.info/repeat.html.
You might try starting your regex with
^(http://)?(www\.)?
And then the rules to match the rest of a URL.
$re = '/http:\/\/mywebsite\.com\/((?:http:\/\/)?[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*(?:\.[0-9A-Za-z]+(?:-+[0-9A-Za-z]+)*)+(?:\/.*)?)/';
https://regex101.com/r/x6vUvp/1
Obeys the DNS rule that hyphens must be surrounded. Replace http with https? to allow https URLs as well.
According to the list of TLDs at Wikipedia there are at least 1519 of them and it's not constant so you may want to give the domain its own capture group so it can be verified with an online API or a file listing them all.
Here is my two cents :
$regex = "/http:\/\/mywebsite\.com\/((http:\/\/|www\.)?[a-z]*(\.org|\.co\.uk|\.com).*)/";
See the working exemple
But I'm sure you can do better !
Hope it helps.
I have a regex that needs to match up a specific url and load some configuration based on that.
Basically What I am have is /*[^(search)].php/ This regex needs to match every url that has .php in the end but parameters may be present (something.pgp?t=19) except for the urls that have search.php
For example
1. http://www.example.com/discuss/viewtopic.php?t=19
2. http://www.example.com/discuss/viewforum.php?f=8
3. http://www.example.com/discuss/search.php?f=8
Among the above three urls the regular expression needs to be able to match 1 & 2 but not 3.
Any help is much appreciated thanks.
EDITED
However it should not be matching any other urls that does not include .php in it.
www.example.com/something should not be matched.
try this:
/.+?(?<!search)\.php(?<params>.*)/
the key is the non-greedy 'anything' .+? : it crawls up the string one by one, always checking for "look behind you and don't see 'search', followed by .php: (?<!search)\.php, followed by the named group which are the optional query string params.
Note that this simple regex is pretty permissive, and assumes that .php alone signifies a "php URL" - you could get crazy complicated validating URL's.
Just use two different location (make sure search.php is before general *.php)
location ~ /search\.php$ {
# config for phusion-passenger
}
location ~ \.php$ {
# config for php-fpm
}
Nginx strip off request parameters while searching for match, so you don't have to care about them.
Problem in /*[^(search)].php/
[^ ] negates of character class
so [^(search)] here would match anything other than ( or s or e or a etc
Solution
You can use a look behind assertion as
^.*(?<!search\.php)\?([^=]=)+\d+$
will match 1 and 2
Example : http://regex101.com/r/bJ3vG1/4
What it doess?
(?<!search\.php) negative lookbehind. asserts that the regex is presceded not by search.php
\?[^=]=\d+ matches parameters
Edit
If the parameter part is optional, a lengthier regex would do the purpose
.*(?<!search\.php)\?([^=]=)+\d+$|^[^?]*(?<!search\.php)$
Example : http://regex101.com/r/bJ3vG1/3
I'm trying to create a snippet of regex that will match a URL route.
Basically, if I have this route /users/:id I want /users/100 to match, but /users/100/edit not to match.
This is what I'm using now: users/(.*)/ but because of the greedy match it's matching regardless of what's after the user ID. I need some way of "breaking" the match if there's an /edit or something else on the end of the route.
I've looked into the Regex NOT operator but with no luck.
Any advice?
Are you just trying to collect digits?
You could use users/(\d*)/
And this one is how you would do it if you wanted to collect everything until a /, and it uses a NOT, ^/users/[^/]*$
You can use negative lookahead:
users/(.*)/(?!edit)
This will always require a trailing slash however. Maybe a better solution would be:
users/(\d+)(?!/edit)
See this post for more information.