NGINX Rewrite Question
I looked at the NGINX wiki and found some examples on how to use the rewrite module for redirecting a HTTP request. I seem to have stumbled upon a problem though. The code is below.
server {
listen 80;
server_name unknown-server.com;
rewrite ^ http://www.unknown-server.com$request_uri? permanent;
access_log /srv/www/unknown-server.com/logs/access.log;
error_log /srv/www/unknown-server.com/logs/error.log;
root /srv/www/unknown-server.com/public_html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
if ($uri !~ "^/img/") {
fastcgi_pass 127.0.0.1:9000;
}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/unknown-server.com/public_html$fastcgi_script_name;
}
}
When I visit unknown-server.com it will redirect to
What I would like to have is a redirect of
How can I do this?
Thanks in advance.
[NOTE]:
Right now the NGINX server has unknown-server.com set to accept both unknown-server.com and
2 Replies
server {
listen 80;
server_name unknown-server.com;
rewrite ^ http://www.unknown-server.com$request_uri? permanent;
}
server {
listen 80 default;
server_name www.unknown-server.com;
access_log /srv/www/unknown-server.com/logs/access.log;
error_log /srv/www/unknown-server.com/logs/error.log;
root /srv/www/unknown-server.com/public_html;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
if ($uri !~ "^/img/") {
fastcgi_pass 127.0.0.1:9000;
}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/unknown-server.com/public_html$fastcgi_script_name;
}
}