URL Rewrite Pattern to exclude application name from path - regex

I'm trying to use the IIS 7 URL Rewrite feature for the first time, and I'm having trouble getting my regular expression working. It seems like it should be simple enough. All I need to do is rewrite a URL like this:
http://localhost/myApplication/MySpecialFolder
To:
http://localhost/MySpecialFolder
Is this possible? I want the regular expression to ignore everything before "myApplication" in the original URL, so that I could use "http://localhost" OR "http://mysite", etc.
Here's what I've got so far:
^myApplication/MySpecialFolder$
But using the "Test Pattern..." feature in IIS, it says my patterns don't match unless I supply "myApplication/MySpecialFolder" exactly. Does anyone know how I can update my regular expression so that everything prior to "myApplication" is ignored and the following URLs will be seen as a match?
http://localhost/myApplication/MySpecialFolder
http://mysite/myApplication/MySpecialFolder
Many thanks in advance!
SOLUTION:
I needed to change my regex to:
myApplication/MySpecialFolder
Without the ^ at the beginning and without the $ at the end.

Your regular expression is correct, the pattern will be matched against path starting after the first slash after the domain.
So only bold part will be used for matching: http://localhost/myApplication/MySpecialFolder
To limit the rewriting to specific domain you have to use Conditions section with Condition input = {HTTP_HOST}

Unless there is something radically different with regexes in IIS, you would want to take out the anchor (^) at the beginning to match.
myApplication/MySpecialFolder$
The carat ^ tells it that that is the beginning of the string and the dollar sign $ tells it to match the end. A regex like abc finds "abc" anywhere in the string, ^abc matches strings that start with "abc", abc$ matches strings that end with "abc", and ^abc$ only matches when the whole string is "abc".

Related

Laravel Routes Regular Expression start with # character

I need to create a route that responses to any string starting with '#' character. Routes like following examples :
www.mywebsite.com/#john
www.mywebsite.com/#jack
www.mywebsite.com/#something
So I wrote:
Route::get('{something}','SomeController#someMethod')->where('something','/#^/');
But when I test it, I face 404 not found found page.
what is the correct regular expression for this?
Route::get('/{tag}', 'SomeController#someMethod')->where('tag', '^#.*');
This will also work:
Route::get('#{something}', 'SomeController#someMethod');
You can write this
Route::pattern('tag', '#[a-zA-Z]');
Route::get('{tag}', 'SomeController#someMethod');
This way you seperate the logic of the regex and the route and it will work as you want
Note the #^ pattern means # should be followed with the beginning of string, which is not possible, and the pattern never matches any string. The '^#' pattern asserts the position at the start of the string, and only there does it try to match #.
Also, the usual / regex delimiters should be removed from this pattern as they are treated as part of the pattern here.
So, in your case you may just swap the anchor and the # char:
Route::get('{something}','SomeController#someMethod')->where('something','^#');

What's the right regular expression to match the exact word at the end of a string and excluding all other urls with more chars at the end?

I have to match an exact string at the end of a url, but not match all other urls that have more characters after that string
I can better explain with example.
I need to match the url having the string 'white' at its end: http//mysite.com/white
But I also need to not match urls having one or more characters postponed to it, like http//mysite.com/white__blue or http//mysite.com/white/yellow or http//mysite.com/white/
How to do that?
Thanks
Regex to match any url*
^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?$
Regex to match a url containing white in the end
^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?white$
You can check the regex here
From regexr.com
It does not match urls(which are not valid anyway) like
httpabrakadabra.co//
http:google.com
http://no-tld-here-folks.a
http://potato.54.211.192.240/
Based on your limited sample inputs, I'd say you could get away with this very minimal pattern:
^http[^\s]+white$
However, depending on what you are truly trying to achieve, what language/function you are implementing this pattern with, and what the full input string looks like, this pattern may need to be refined.
It would be best if you would improve your question to include all of the above relevant information.

Regex to match all urls, excluding .css, .js recources

I'm looking for a regular expression to exclude the URLs from an extension I don't like.
For example resources ending with: .css, .js, .font, .png, .jpg etc. should be excluded.
However, I can put all resources to the same folder and try to exclude URLs to this folder, like:
.*\/(?!content\/media)\/.*
But that doesn't work! How can I improve this regex to match my criteria?
e.g.
Match:
http://www.myapp.com/xyzOranotherContextRoot/rest/user/get/123?some=par#/other
No match:
http://www.myapp.com/xyzOranotherContextRoot/content/media/css/main.css?7892843
The correct solution is:
^((?!\/content\/media\/).)*$
see: https://regex101.com/r/bD0iD9/4
Inspirit by Regular expression to match a line that doesn't contain a word?
Two things:
First, the ?! negative lookahead doesn't remove any characters from the input. Add [^\/]+ before the trailing slash. Right now it is trying to match two consecutive slashes. For example:
.*\/(?!content\/media)[^\/]+\/.*
(edit) Second, the .*s at the beginning and end match too much. Try tightening those up, or adding more detail to content\/media. As it stands, content/media can be swallowed by one of the .*s and never be checked against the lookahead.
Suggestions:
Use your original idea - test against the extensions: ^.*\.(?!css|js|font|png|jpeg)[a-z0-9]+$ (with case insensitive).
Instead of using the regular expression to do this, use a regex that will pull any URL (e.g., https?:\/\/\S\+, perhaps?) and then test each one you find with String.indexOf: if(candidateURL.indexOf('content/media')==-1) { /*do something with the OK URL */ }

Negative look-ahead expression

I'm writing a Perl script that performs different actions on hosts depending on which patterns match the FQDN. I've been struggling to find a regular expression that skips hosts that have the string 'test' in the domain name.
These host names represent the four host name types I'm dealing with:
node01.prod.com
node01.test.com
node02.dmz.prod.com
node02.dmz.test.com
The following expression matches the host name pattern I'm trying to skip:
/\w\.test/
But, none of the negative look-ahead expressions I've tried will skip the host names with 'test'. For example, this expression:
/\w\.(?!test)/
matches/passes all four host name types, including the two that contain the string 'test'.
What's really driving me crazy is that if I hard code part of the host name, the negative look-ahead expression does skip the full host name:
/node01\.(?!test)/ # only matches node01.prod.com
I'm surely missing something terribly obvious - any suggestions?
The problem is that you're putting the negative lookahead after your match, which allows it to match a partial node name even if it has the word test in it somewhere.
This expression will match any string that doesn't contain test:
(?!.*test)^.*$
Online demonstration:
http://regex101.com/r/rZ0vO2
You can use this regex:
/\w\.(?!test).+/
Your negative lookahead is correct but your regex is not really matching anything after dot.
Something like the following should do it:
/(?:\w+\.(?!test))+\w+/
Depending on how you are using the regex, you may also need some anchors to prevent this from matching after test:
/^(?:\w+\.(?!test))+\w+$/
This works by putting the negative lookahead within a repeating group, so that it is checked after each . that is matched in your regex.
For example: http://rubular.com/r/TeUYi9EIEL
Did you try this pattern?
/(?!.*\.test\.)^.+/

Correct regex / mod-rewrite syntax for this url

Hi I am having a little difficulty working out this mod rewrite rule / regex correctly.
I have a url format like this:
www.site.com/some-page-title-here-cb384
www.site.com/another-page-title-here-cb385
And I'd like to find only the numbers after each 'cb' only if the url contains a 'cb' after the last hyphen in each string.
I have:
.*?([0-9]+)$
Which matches the last set of numbers but I need to be more specific in saying only if the last section of the url contains the pattern '-cb'.
Try this:
.*-cb(\d+)$
This one should work and you should find the numbers in $1.
Let me be more specific. Your regexp (and mine above) doesn't match only the last part of the string, but matches the whole string. If you want to match only the last part, you should write it without .*:
-cb(\d+)$