NGINX 将子域名重定向到子目录

NGINX 将子域名重定向到子目录

我知道这个问题肯定已经被回答了一百万次,但我似乎无法让它发挥作用

我想将子域名重定向到子目录。

我目前正在使用

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

如果我导航到 sub.domain.com,我会被重定向到http://www.domain.com


编辑

这是我的完整配置。我更改了实际的域和子目录

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;
}

}

答案1

server {
    server_name sub.domain.com;
    return 301 http://www.domain.com/sub-directory$request_uri;
}

答案2

我认为当您使用通用 sub.domain.com 时,您尝试执行的一些操作会丢失 - 要将子域名专门发送到同名目录,您有 2 个选项:

对每一个进行硬编码

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}";
}

或者用正则表达式匹配(效率较低但更简单)

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}";
}

请注意,$uri 包含前导斜杠,这与 Apache 的 mod_rewrite 不同。

答案3

只需重写即可轻松完成...

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

只需确保您的“正常”server条目也监听此域名即可

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;
    }
    ...
  }

  ...

答案4

这应该可行。请阅读NGINX 陷阱和常见错误!您将免费获得所投入的时间和几天的时间;)

server { 
   server_name OLD.example.com;
   return 301 $scheme://example.com/NEW$request_uri;
}

相关内容