remove first folder in nginx - regex

I have some URLs like:
http://example.com/username/file.zip
http://example.com/username/videos/aaa.avi
http://example.com/username/videos/abc/asdfdef/aaa.avi
the real path of files are:
/file.zip
/videos/aaa.avi
/videos/abc/asdfdef/aaa.avi
so basically I need to remove first folder in the URL
I tried to use this rewrite rule :
rewrite ^/.*/(.*)$ /$1 last;
but its remove all folders and grep just the filename, it's work just for first URL and i get 404 error for rest of them
- P.S: the username could be anything

i didnt test it but based on that nginx uses pcre library i think
rewrite ^/.*?/(.*)$ /$1 last;
would work.
.*? matches any character between zero and unlimited times, as few times as possible, expanding as needed (lazy)

Related

How to match everything after slash to use as an nginx rewrite?

I have these test cases:
/test
/test/
/test/whatever
I would like to write an nginx rewrite rule that only targets /test/whatever
I currently have
rewrite ^/(test/)(.*) /some-other-page; but this targets all the above cases.
Any ideas?
The ^/(test/)(.*) will match /test/ because .* can match an empty string.
You may use
^/test/(.+)
The .+ will require at least 1 char after /test/.

Matching URI with regex find replace

I'm still learning Regex operations and I couldn't figure out how I can execute a regex that replace in a URI the "/" with "-" in an Apache rewrite rule expression. Assuming that It would be possible for as many "/" as existing in the incoming URI.
I tried ^\/?(([a-z0-9_\.-]+)\/)+$ and tested with $1-$2 for example1/example2 but i didn't work.
Can anyone help me with this ?
Thanks
The following lines in mod_rewrite in .htaccess in the root folder, or in the vhost configuration file in your Apache :
RewriteEngine On
RewriteRule (.*)([^\/]+)\/([^\/]+)\/?$ http://%{HTTP_HOST}$1$2-$3
will rewrite
http://%{HTTP_HOST}/example1/example2
as
http://%{HTTP_HOST}/example1-example2
and
http://%{HTTP_HOST}/example1/example2/example3/example4
as
http://%{HTTP_HOST}/example1-example2-example3-example4
Why not just use
\/ '-'
preferably with the 'g' flag to replace every slash in the Uri. That way (in javascript)
"example1/example2".replace(/\//, '-');
returns
"example1-example2"

Redirect Match with excluding URLs doesnt work

I use a RedirectMatch rule which should exclude the following two URLs:
citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen
citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen
I use this rule with regex, but I get a 500 Internal Server Error:
RedirectMatch 301 /citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$ /citycards/citycards-locations/muenchen/$1
Any ideas why it doesn't work?
Your rule currently is: (broken down to multiple lines for better display/understanding):
RedirectMatch
301
/citycards/citycards-locations/muenchen/((?!citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen|citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$
/citycards/citycards-locations/muenchen/$1
Basically, your regex says that:
match /citycards/citycards-locations/muenchen/
which is not followed by either of the following
citycards/citycards-locations/munche‌​n/citycards-trachtenvogl-reichenbachst‌​r-47-munchen,
citycards/citycards-location‌​s/munchen/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen (it has a space after 18‌​-munchen)
match everything until the end of URI
and redirect the matched URI to: /citycards/citycards-locations/muenchen/$1 which is basically the same URL that was matched against.
I see 2 issues.
If the blank space in your negative lookahead is not considered as a part of the pattern, you are essentially passing 4 arguments to RedirectMatch directive, leading to status 500 error
If the pattern is getting parsed correctly, you have an infinite redirection loop.
/citycards-4-you-munchen-hirtenstrasse-18‌​-munchen ).+)$
You have an unescaped space near the end of your RewriteRule pattern. This will certainly cause a 500 error since the space is a delimiter and the arguments will not match up (invalid flags).
It looks like this is a typo and should simply be removed?

Redirect all URLs starting with STRING that does NOT include a SUBSTRING

I need some help with regex.
I'm building some 301 rules for an .htaccess
I need to redirect all urls starting with a specific string excluding one that has a given word in the match-all part
this is the simple rule I'm using:
/my/sample/url/(.*)
I need to edit the (.*) part to say: anything except if contains "foobar"
if contains "foobar" I need a different 301 rule
This looks like is working:
^(?!.*foobar)/my/sample/url/(.*)
does anybody have a better solution?

.htaccess, mod_rewrite, regex with directory and filetype

I want to do a rewrite with the following conditions:
Directory is /images
file has a .jpg, .png or .gif extension
I want to redirect to the following
/images/?file=filename.extension
This is not working:
RewriteRule /images/(.*\.jpg|png|gif) /images/?file=$1
Example:
/images/example.jpg => /images/?file=example.jpg
Thanks
I think you need parens to group the extensions.
/images/(.*\.(jpg|png|gif))
In the .htaccess file, the path is stripped of the leading slash so you need to start with a images/ instead of /images/. Also, you can start with a ^ to match the beginning of the path so that paths like /blahblah/images/something.gif won't get rewritten. Finally, your paren will match foo.jpg but not foo.png or foo.gif. Try this instead:
RewriteRule ^images/(.+\.(jpg|png|gif)) /images/?file=$1