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

i could use a little help with rewrite and nginx... Basically the structure of my App looks like this

Headdirectory --

So i need to redirect into the WWWDirectory when i open the Headdirectory. In Apache its done with a htaccess and the following Content :

RewriteEngine On
RewriteRule ^(.*) www/$1

I already tried in Nginx :

location /Headdirectory {    
rewrite ^/(.*) /www/$1;

And i tried to create an Alias but that didnt work...

Would be nice if someone could help me out.

Have a nice Day

share|improve this question
up vote 1 down vote accepted

Here is how it could be done in Nginx...

server {
  root /path/to/;
  index index.php; # or index.html or something that suits the application

  location ~ /Headdirectory/WWWDirectory/ {
    # do some stuff with WWWDirectory
    # something like try_files $uri =404;

  location ~ /Headdirectory/ {
    rewrite ^/Headdirectory/(.*)$ /Headdirectory/WWWDirectory/$1 permanent;

  # other blocks and directives

  location / {
    # do stuff with the root
    # such as try_files $uri =404;


share|improve this answer
That worked , Thank you very much. I'm still pretty new to this regular expression stuff :) – Robin Jun 21 '12 at 13:05
You're welcome @Robin. – Pothi Jun 21 '12 at 15:28

It is a little bit confusing question...

Your configuration is, that if someone will try, it will be rewriten to, which is not you want, I pretend...

Try this:

root /full/path/to/the/Headdirectory
location / {
     rewrite ^/(.*) /www/$1;


I'm sorry, you will need this config block:

location /www {

It will stop rewriting...

share|improve this answer
Thank you in Advance. Sorry , its a bit early for me. I need to open the Headdirectory , because my App requires it, but the redirect needs to go to the www directory. When i try this i get in a redirect loop. www/www/www/www/www/www Basically if someone goes to the – Robin Jun 21 '12 at 9:39
Please, try add a new block to the config... – Jan Marek Jun 21 '12 at 9:50
Sorry, this block existed with the old path ..^^ Now Firefox and Chrome say that there are too many redirects. Errorlog is empty. Thanks , i only want to migrate to nginx :/ – Robin Jun 21 '12 at 10:03

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.