我知道这肯定已经被回答了一百万次,但是我似乎无法得到它的工作
我想将一个子域redirect到一个子目录。
我目前正在使用
server { server_name sub.domain.com; rewrite ^(.*) http://www.domain.com/sub-directory permanent; }
如果我导航到sub.domain.com我redirect到http://www.domain.com
编辑
这是我的完整configuration。 我已经改变了实际的域和子目录
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; }
}
server { server_name sub.domain.com; return 301 http://www.domain.com/sub-directory$request_uri; }
这可以通过重写来完成。
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; } ... } ...
我认为你使用通用的sub.domain.com会丢失一些你正在做的事情 – 特别是把子域名发送到同名的目录,你有两个select:
硬编码每一个
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}"; }
或与正则expression式匹配(效率较低但较容易)
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不同的前导斜杠。
这应该工作。 请阅读NGINX陷阱和常见错误 ! 你将获得投资时间和几天的免费;)
server { server_name OLD.example.com; return 301 $scheme://example.com/NEW$request_uri; }