Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know this must have been answered a million times but I can't seem to get it to work

I would like to redirect a subdomain to a subdirectory.

I am currently using

server {
server_name sub.domain.com;
rewrite ^(.*) http://www.domain.com/sub-directory permanent;
}

If I navigate to sub.domain.com I am redirected to http://www.domain.com


EDIT

Here's my full config. I have changed the actual domains and sub directories

server {
server_name forum.domain.com;
rewrite ^(.*) http://www.domain.com/forum permanent;
}

server {
# Listen on port 80 as well as post 443 for SSL connections.
listen xx.xxx.xxx.xxx:80;


server_name domain.com www.domain.com;


# Path to the files in which you wish to
# store your access and error logs.
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

root /usr/share/nginx/www;

location /wordpress-blog {
    index index.php;
    try_files $uri $uri/ /wordpress-blog/index.php?args;
}

#root is a magento ecommerce install
location / {
    index index.htm index.php;
    try_files $uri $uri/ @handler;
}


    #this part makes all pretty urls work for my simple machines forum
location /forum/
{

    index index.php index.html;

    if (!-e $request_filename)
    {
            # Rules for: profiles
            rewrite ^/burton-snowboard-forum/profile/([^/]+)/?$ "/burton-snowboard-forum/index.php?pretty;action=profile;user=$1" last;

            # Rules for: actions
            rewrite ^/burton-snowboard-forum/(activate|admin|ads|announce|attachapprove|ban|boardrecount|buddy|calendar|clock)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(collapse|convertentities|coppa|credits|deletemsg|detailedversion|display|dlattach|editpoll|editpoll2)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(emailuser|featuresettings|findmember|groups|help|helpadmin|im|jseditor|jsmodify)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(jsoption|lock|lockvoting|login|login2|logout|manageboards|managecalendar|managesearch|manageattachments|maintain|markasread|mascot)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(membergroups|mergetopics|mlist|moderate|modifycat|modifykarma|movetopic|movetopic2|news|notify)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(notifyboard|optimizetables|openidreturn|packages|permissions|pm|post|postsettings|post2|printpage|profile|quotefast)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(quickmod|quickmod2|recent|regcenter|register|register2|reminder|removepoll|removetopic2)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(repairboards|reporttm|requestmembers|restoretopic|reports|search|search2|sendtopic|serversettings|smileys|smstats|suggest)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(spellcheck|splittopics|stats|sticky|theme|trackip|about:mozilla|about:unknown)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(unread|unreadreplies|verificationcode|viewErrorLog|viewmembers|viewprofile|vote|viewquery|viewsmfile|who)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;
            rewrite ^/burton-snowboard-forum/(\.xml|xmlhttp)/?$ "/burton-snowboard-forum/index.php?pretty;action=$1" last;

            # Rules for: boards
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1.0" last;
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1.$2" last;

            # Rules for: topics
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1;topic=$2.0" last;
            rewrite ^/burton-snowboard-forum/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/?$ "/burton-snowboard-forum/index.php?pretty;board=$1;topic=$2.$3" last;

            rewrite ^/(.*)$ /index.php?params=$1 last;
    }
}

#Magento configurations

# Deny access to specific directories no one
# in particular needs access to anyways.
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }

# Allow only those who have a login name and password
# to view the export folder. Refer to /etc/nginx/htpassword.
location /var/export/ {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    autoindex on;
}

# Deny all attempts to access hidden files
# such as .htaccess, .htpasswd, etc...
location ~ /\. {
     deny all;
     access_log off;
     log_not_found off;
}

# This redirect is added so to use Magentos
# common front handler when handling incoming URLs.
location @handler {
    rewrite / /index.php;
}

# Forward paths such as /js/index.php/x.js
# to their relevant handler.
location ~ .php/ {
    rewrite ^(.*.php)/ $1 last;
}

# Handle the exectution of .php files.
location ~ .php$ {
    if (!-e $request_filename) {
        rewrite / /index.php last;
    }
    expires off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param HTTPS $fastcgi_https;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param MAGE_RUN_CODE btn;
    fastcgi_param MAGE_RUN_TYPE website;
    include fastcgi_params;
}

}

share|improve this question

closed as not a real question by Michael Hampton, mgorven, growse, kce, mdpc May 22 '13 at 1:51

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
It works. What exactly is the problem? –  Michael Hampton Sep 12 '12 at 10:13
    
Post your full config. –  quanta Sep 12 '12 at 10:28
add comment

4 Answers 4

server {
    server_name sub.domain.com;
    return 301 http://www.domain.com/sub-directory$request_uri;
}
share|improve this answer
    
Sorry, I'm not having any luck with this. sub.domain.com loads the content that www.domain.com would load. I need it to go to www.domain.com/my-sub-directory –  John Sep 14 '12 at 13:00
add comment

I think some of what you're trying to do is being lost when you use the generic sub.domain.com - to specifically send the sub-domain to the directory of the same name you have 2 options:

Hard code each one

server {
  server_name sub1.domain.com;
  return 301 "http://domain.com/sub1${uri}";
}

server {
  server_name sub2.domain.com;
  return 301 "http://domain.com/sub2${uri}";
}

Or match with regular expressions (less efficient but easier)

server {
  server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
  return 301 "http://domain.com/${sub}${uri}";
}

Note that $uri includes the leading slash which is different to Apache's mod_rewrite.

share|improve this answer
1  
Please, do not use rewrite instead of simple return. –  VBart Sep 12 '12 at 14:00
    
I just learnt something. Thank you –  Arran Schlosberg Sep 12 '12 at 21:15
    
What is this _ in your server_name? btw, domain matching are always case insensitive. Losing request arguments isn't a good idea (replacing $uri with $request_uri should fix it). –  VBart Sep 13 '12 at 8:03
    
The _ is a remnant of my older nginx configurations for catchall blocks. It isn't relevant here so I've removed it. –  Arran Schlosberg Sep 13 '12 at 8:17
    
Both these provided solutions don't work for me. sub.domain.com loads the content that www.domain.com would load. I need it to go to www.domain.com/my-sub-directory –  John Sep 14 '12 at 12:28
add comment

This could just be done very simply with a rewrite ...

if ($host !~* ^www\.example\.com) {
  rewrite ^(.*)$ http://www.example.com$1 permanent;
}

Just make sure your "normal" server entry also listens for this domain name

server {

  # Listen on port 80 as well as post 443 for SSL connections.
  listen xx.xxx.xxx.xxx:80;

  server_name example.com www.example.com mysubdomain.example.com;

  location / {
    if ($host !~* ^www\.example\.com) {
      rewrite ^(.*)$ http://www.example.com$1 permanent;
    }
    ...
  }

  ...
share|improve this answer
add comment

This should work. Please read http://wiki.nginx.org/Pitfalls! You will get back the invested time and a few days for free ;)

server { 
   server_name OLD.example.com;
   return 301 $scheme://example.com/NEW$request_uri;
}
share|improve this answer
add comment

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