Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Right now, I have this:

location ~ ^/phpmyadmin/(.*)$
        alias /home/phpmyadmin/$1;

However, if I visit (note the lack of trailing slash), it won't find what I'm looking for and 404. I assume because I don't include the trailing slash. How can I fix this?

share|improve this question
up vote 16 down vote accepted

It might be in the regular expression that you're using --

location ~ ^/phpmyadmin/(.*)$

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:

location ~ /phpmyadmin/?(.*)$ {
    alias /home/phpmyadmin/$1;

The question mark is a regular expression quantifier and should tell nginx to match zero or one of the previous character (the slash).


share|improve this answer
Thank you! And thank you for the explanation, as well. Will come in handy in the future. – Rob Apr 3 '12 at 6:08
The pattern isn't quite correct. It can also match path like "/phpmyadmin1234", obviously that's no what you want. @kbec's solution is the right one. – Meow Aug 20 '15 at 12:10

Why wouldn't you just use

location /phpmyadmin {
    alias /home/phpmyadmin;


share|improve this answer
Because that wasn't working for me. See here:… – Rob Apr 3 '12 at 5:35
@Rob Did you try it without the trailing slash on the alias directive? – Shane Madden Apr 3 '12 at 17:04

The better solution:

location ~ ^/phpmyadmin(?:/(.*))?$ {
    alias /home/phpmyadmin/$1;

Ensure that server has permissions to /home/phpmyadmin first.

share|improve this answer

This redirect will only rewrite URLs with and without the trailing slash. Anything that comes after the slash won't be overwritten. => redirected to => redirected to => not redirected

server {
  # 301 URL Redirect
  rewrite ^/location/?$ /new/location permanent;
share|improve this answer

Have you tried using try_files directive?

try_files $uri $uri/ =404;
share|improve this answer

I did it like that

rewrite ^/promo/?$;
location /promo/ {
    root /var/www;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.