Right now I have this config:
location ~ ^/phpmyadmin/(.*)$
{
alias /home/phpmyadmin/$1;
}
However, if I visit www.mysite.com/phpmyadmin
(note the lack of trailing slash), it won't find what I'm looking for a 404. I assume because I don't include the trailing slash. How can I fix this?
The better solution:
Ensure that server has permissions to
/home/phpmyadmin
first.Explanation of difference with accepted answer:
It's all about regular expressions.
First of all, the
^
char means that you want to match from beginning of string and not somewhere in the middle. The$
at the end means matching to the end of the string.The
(?:)
means non-capturing group - we don't want it in the capturing results, but we want to simple group some chars. We group it like this, because we want the/
char to be a nonsignificant part of the child path, and not a significant part of the parent path.It might be in the regular expression that you're using --
The above will match /phpmyadmin/, /phpmyadmin/anything/else/here, but it won't match /phpmyadmin because the regular expression includes the trailing slash.
You probably want something like this:
The question mark is a regular expression quantifier and should tell nginx to match zero or one of the previous character (the slash).
Why wouldn't you just use
?
I know this is an old question, but for anybody that ends up here via Google, I solved it the following way (variation of @kbec's one, which was quite good):
This will capture any variation of
/foo
and redirect it to/bar
on another url (including parameters). I am showing it with aproxy_pass
directive but it would also work withalias
./foo
->/bar
/foo/
->/bar/
/foo/sub
->/bar/sub
/foo/sub1/sub2/?param=value
->/bar/sub1/sub2/?param=value
It works because
$1
will optionally capture the subresources plus the leading slash, so it won't capture things like/fooextra/
. It will also redirect a present or non-present ending slash properly.EDIT: Check comments for caveats about this approach.
This redirect will only rewrite URLs with and without the trailing slash. Anything that comes after the slash won't be overwritten.
domain.com/location => redirected to domain.com/new/location
domain.com/location => redirected to domain.com/new/location
domain.com/location/other => not redirected
When using proxy_pass with a location given by a prefix string rather than a regular expression Nginx will take care of the trailing slash for you, as described in the docs:
Have you tried using
try_files
directive?Why not put two rewrites simply:
I did it like that
If you need also
proxy_pass
it is better to pass arguments too:That said, my personal suggestions is to NEVER use
phpmyadmin
in production. You will understand why by checking the access logs of your public web-server... (lot of bots trying to exploit phpmyadmin)