I am not very familiar with Regular expression, but I am asked to modify a lighttpd rewrite rule.
url.rewrite = (
"^/(.+)/?$" => "/index.php/$1"
)
I want to exclude a path from the above greedy pattern, so that it won't fall back to index.php.
In words, it is simple: Match anything other than "statistics". But I just couldn't get it right in regex.
For example:
http://www.foo.com/anything/ → index.php/anything
http://www.foo.com/statistics/ → statistics/index.php
Would you please show me a hint to achieve that?
Thank you!
You probably want to use a negative lookahead. Something like
"^/(?!statistics)(.+)/?$" => "/index.php/$1"
And then you'll need an additional rule for statistics
Related
I want to make a redirection on an url :
/XX/YY/ZZ%3E%3E%3E%3E%3E%3E%3E%3E%3E => /XX/YY/ZZ
I don't find the good regex to remove multi match "%3E" at the end of the url.
Can you help me please ?
This should work (for actual URLs with the indicated kind of suffix):
x = "https://www.test.com/XX/YY/ZZ%3E%3E%3E%3E%3E%3E%3E%3E%3E"
s.gsub(/(%3E)+$/,"")
Try this pattern:
\/[\w]{2}
You can test it online
Thank you Human and Drux !
You really helped me to find the solution :
r301 %r{^/XX/([\w\/]*)(%3E)+$}, '/XX/$1'
For the last couple hours I tried to create a ReWrite Rule in IIS which does meet my requirements, but I just don't get it. So maybe someone can help me out :-)
What I have till now is the following:
Rewrite URL
index.php?page={R:1}¶m={R:2}
RegEx Pattern
^(.*)/([0-9]+)
This is how I'd like to write my URLs at the end:
http://url.com/path1/path2/path3/param/ for example http://url.com/news/detail/1/
In this example I should have "news/detail" in "?page" and "1" in "?param".
With the rule I created so far that seems to work quite good, as long as I have a number at the end (param).
My only problem is that I want to make the number (param) optional.
Thanks a lot for your support.
I can't come up with something more permissive than this:
^([a-z-/]+)([0-9])?
Base on your comment Paths never have a number in it, I went a bit further and allowed only characters from a to z (use the ignore case option if needed).
This rule will match any of the following url:
news/detail/1/ => {R:1} = news/detail/ and {R:2} = 1
news/detail/1 => {R:1} = news/detail/ and {R:2} = 1
news/detail/ => {R:1} = news/detail/ and {R:2} is empty
news/detail => {R:1} = news/detail and {R:2} is empty
You probably will have to deal with the trailing / in your code.
The limitation comes from the fact that as far as I know, the regex in the rewrite rule doesn't support negative lookahead/lookbehind pattern.
To allow for the final match capture to be optional put a ? by it. Also specify that it will be at the by anchoring it with the end character $.
^(.*)/?(\d+)?$
I have also made the final / optional since if there is no digit you don't want match to fail if it does not have a / at the end (which should be optional).
I'm trying to rewrite urls that have an image in them but not 'php', for example:
I want to match this:
http://domain.com/trees.jpg
and rewrite it to this:
http://domain.com/viewimg.php?image=trees.jpg
But not match this:
http://domain.com/index.php?image=trees.jpg
because it has 'php' in it, so it shouldn't do any rewriting at all to that url.
I'm no good with regex but I've been trying a number of variations and none have worked.
Here's an example of one variation I've tried:
url.rewrite-once = (
"(?!php\b)\b\w+\.(jpg|jpeg|png|gif)$" => "/viewimg.php?image=$1.$2"
)
Any help would be greatly appreciated, thanks.
This should get you started. It will fail matches where there is the string \.php anywhere behind \w+\.(jpg|jpeg|png|gif)
Pattern: (?<!.*\.php.*)\b(\w+\.(jpg|jpeg|png|gif))$
Replace: viewing.php?image=$1
Tested:
http://domain.com/trees.jpg ---> http://domain.com/viewing.php?image=trees.jpg
http://domain.com/index.php/trees.jpg ---> http://domain.com/index.php/trees.jpg
http://domain.com/index.php/image=trees.jpg ---> http://domain.com/index.php/image=trees.jpg
Edit:
Above uses non-constant-length lookbehind, which is apparently not supported on many platforms. Without this I don't know if you can codify "match XYZ when ABC is nowhere in the string behind it" in pure regex. Maybe someone with more regex-fu than me knows a way.
As a somewhat less general solution, this will check only if the fixed string .php?image= does not come right before the image name:
Pattern: (?<!\.php\?image=)\b(\w+\.(jpg|jpeg|png|gif))$
Replace: viewing.php?image=$1
I'm trying to create a regex that takes a filename like:
/cloud-support/filename.html#pagesection
and redirects it to:
/cloud-platform/filename#pagesection
Could anyone advise how to do this?
Currently I've got part-way there, with:
"^/cloud-support/(.*)$" => "/cloud-platform/$1",
which redirects the directory okay - but still has a superfluous .html.
Could I just match for a literal .html with optional #? How would I do that?
Thanks.
Maybe something like this:
"^/cloud-support/(.*?)(\.html)?(#.+)$" => "/cloud-platform/$1$3"
where the first group is a non-greedy match (.*?)
"^/cloud-support/(\w+).html(.*)" => "/cloud-platform/$1$2"
Would something like this work?
"^/cloud-support/([^.]+)[^#]*(.*)$" => "/cloud-platform/$1$2"
Can you try the regex
"^/cloud-support/(.*)\.html(#.*)?$"
The \.html part matches .html while (#.*)? allows an optional # plus something.
I would like some help matching the following urls.
/settings => /settings.php
/657_46hallo => /657_46hallo.php
/users/create => /users.php/create
/contact/create/user => /contact.php/create/user
/view/info.php => /view.php/info.php
/view/readme - now.txt => /view.php/readme - now.txt
/ => [NO MATCH]
/filename.php => /unknown.php
/filename.php/users/create => /unknown.php
if the first part after the domain name is a filename ending with ".php"
(see last 2 examples) It should redirect to /unknown.php
I think I need 2 regular expressions
1st should be almost something like: ^/([a-zA-Z0-9_]+)(/)?(.*)?$
2nd to catch the direct filename "/filename.php" or "/filename.php/create/user"
so I can redirect to unknown.php
The 1st regular expression that I got almost works for the first part.
==============================================
request url: http://domain.com/user/create
regex: ^/([a-zA-Z0-9_]+)(/)?(.*)?$
replace http://domain.com/$1.php$2$3
makes: http://domain.com/user.php/create
Problem is it also matches http://domain.com/user.php/create
If someone could help me with both regular expressions that would be great.
If you want to match those .php cases you can try this:
^\/([a-zA-Z0-9_]+)(\/)?(.*)?$
See here on Regexr
If you want to avoid those cases try this:
^/([a-zA-Z0-9_]+)(?!\.php)(?:(/)(.*)|)$
See here on Regexr
The (?!\.php) is a negative look ahead that ensures that there is no .php at this place.
When all you have is a hammer...
While this probably could be solved with a regexp, it is probably the wrong tool for the job, unless you have constraints that MANDATE the use of regexps.
Split the string using '/' as the delimiter, see whether the first component ends with '.php'; if so, reject it, otherwise append '.php' to the first component and join the components back using '/'.