I'm using Nginx and pointing a couple of old domain names to a new site.
The first block in this config works fine for how I need old.domain to behave when redirecting to new.domain.
In the second block, I'm trying to forward any request for oldmediaserver.domain except /robots.txt to the homepage of new.domain. In its current state, every request redirects including for /robots.txt - and I can't work out why.
(The reason for this is I had something indexed by Google from the old domain, and I'm trying to remove it from search results via webmaster tools - that might not work, but that's not what I'm asking for help with here!).
# Old site to new site config
server {
listen 80;
listen [::]:80;
server_name old.domain www.old.domain;
rewrite ^ $scheme://www.new.domain$request_uri permanent;
}
# Media server Redirect and Robots directive
server {
listen 80;
listen [::]:80;
server_name oldmediaserver.domain www.oldmediaserver.domain;
location / {
rewrite / $scheme://www.new.domain/ permanent;
}
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
rewrite ^ $scheme://www.new.domain/ permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/website-name/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name www.new.domain;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# include a file for any 301 redirects
include includes/website-name-redirects;
location /members/ {
try_files $uri $uri/ /index.php?$args;
auth_basic "Members Login";
auth_basic_user_file /var/www/website-name/html/.htpasswd;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
#!!! IMPORTANT !!! We need to hide the password file from prying eyes
# This will deny access to any hidden file (beginning with a .period)
location ~ /\. { deny all; }
}
Thanks for any light you can shed!
rewrite...
statement.www.oldmediaserver.domain/robots.txt
now returns correctly, andwww.oldmediaserver.domain/foo
forwards tonew.domain
's homepage - howeveroldmediaserver.domain/robots.txt
without www still forwards rather than returning the robots directive?location = /robots.txt
=
for exact match, but you set me on the path to the answer (below) - so thanks!