How to make www.sitename.com to www2.sitename.com? - ssl

Sorry to bother you with this, you've been a great community to learn from and I am very thankful. However, I couldn't find a solution to a task I'm having.
I want to change my domain from https://www.sitename.com to https://www2.sitename.com
Do you have any idea how I could do that? Any help would be immensely appreciated!
I am using nginx on ubuntu 16.04.
EDIT: I forgot to mention that I have to use Cloudflare.
My current configuration:
server{
server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;
}
server {
#Ports
listen 80;
listen 443 ssl;
#Server name
server_name www2.example.com www.example.com example.com;
}
So far I am able to open what I want from www2.example.com but I need to redirect www.example.com example.com to www2.example.com

If you want to redirect then, in your nginx.conf you should do this
server {
server_name https://www.sitename.com/; // Old domain
return 301 https://www2.sitename.com/$request_uri; // New domain
}

So I was able to do it like this:
server{
#Ports
listen 80;
listen 443 ssl;
#Server name
server_name example.com www.example.com;
#Return
return 301 https://www2.example.com$request_uri;
}
server {
#Ports
listen 80;
listen 443 ssl;
#Server name
server_name www2.example.com;
}

You can set the first server with the domains you want to redirect
and remove those domains from the second server:
server{
server_name www.example.com example.com ;
rewrite ^/(.*) http://www2.example.com/$1 permanent;
}
server {
#Ports
listen 80;
listen 443 ssl;
#Server name
server_name www2.example.com;
}

Step 1 on CloudFlare in DNS section add A record www2 with your server IP
Step 2 Make your redirect.
you cay use this way
server {
listen 80;
listen 443 ssl;
server_name www.old-name.com;
return 301 $scheme://www.new-name.com$request_uri;
}
Step 3 restart NGINX
sudo service nginx restart

Related

Redirecting www to non-www

I'm trying to redirect all traffic from an HTTP site to an HTTPS site as well as all www traffic to the non-www site. My setup includes an nginx.conf file for the HTTP site which I've added a 301 redirect rule to:
server {
listen 80;
listen [::]:80;
server_name
sub.domain.com
www.sub.domain.com
;
return 301 https://sub.domain.com$request_uri;
...
}
I also have an https nginx.ssl.conf file which looks something like:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub.domain.com www.sub.domain.com;
ssl on;
...
}
With this setup, I'm able to redirect all HTTP traffic to HTTPS but if I try to add a redirect in my nginx.ssl.conf file I receive a "too many redirects" error.
Any suggestions on how I can redirect everything to my HTTPs, non-www site?
You need to split your https block into two domains
server {
listen 80;
listen [::]:80;
server_name
sub.domain.com
www.sub.domain.com
;
return 301 https://sub.domain.com$request_uri;
...
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.sub.domain.com;
return 301 https://sub.domain.com$request_uri;
ssl on;
...
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub.domain.com;
ssl on;
...
}

Redirect HTTP to HTTPS from Nginx is not working

Type: https://example.com => ssl ok
But type: www.example.com and example.com is http no redirect https. (www redirect to non-www).
WordPress Address (URL) and Site Address (URL): https//example.com
/etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl on;
ssl_certificate /etc/nginx/ssl/cert_chain.crt;
#ssl_CACertificate_File /etc/nginx/ssl/example.com.ca-bundle;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
access_log off;
# access_log /home/example.com/logs/access_log;
error_log off;
# error_log /home/example.com/logs/error.log;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
root /home/example.com/public_html;
include /etc/nginx/conf/ddos2.conf;
index index.php index.html index.htm;
server_name example.com;
How to fix it ?
Sorry my bad English, thank you.
This might be caused by the ambiguous server name indeed. Try using the following:
server {
server_name example.com www.example.com;
listen 80;
listen 443 ssl; # Listen for SSL at port 443 as well
# ... other config - certificates and such
# If a user tries to come through http, redirect them through https
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
You can check your nginx configuration by running sudo nginx -t.
I had the similar problem. It was caused by linux firewall (port 80 was disallowed).
Your configuration is not clear, you have at the end a duplicated server_name example.com line.
Try to use this:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
Update this line return 301 https://$server_name$request_uri;
With return 301 https://$http_host$request_uri
i have also the same problem with nginx so I apply these to server to accept only one request which will decide for both http and https
server {
listen 80 ;
listen [::]:80 ;
listen 443 ssl http2 ;
listen [::]:443 ssl http2 ;
server_name example.com www.example.com;
#------ ssl certificates and other config --------
set $https_redirect 0;
#if request came from port 80/http
if ($server_port = 80) {
set $https_redirect 1;
}
# or if the requested host came with www
if ($host ~ '^www\.') {
set $https_redirect 1;
}
#then it will redirects
if ($https_redirect = 1) {
return 301 https://example.com$request_uri;
}
}
by using this I have only server block to hanlde any request
This code will help you redirect to https:
Say you enter http://www.example.com, then it will redirect to https://www.example.com,
if someone enters http:example.com, it will redirect to https://www.example.com
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /usr/local/apache2/htdocs
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost_default_:443>
ServerName www.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</VirtualHost>

How to redirect www to non-www and http to https using nginx and cloudflare?

I am using the following to redirect all users to https and to non-www:
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
access_log /var/log/nginx/example.com.access.log rt_cache;
error_log /var/log/nginx/example.com.error.log;
root "/usr/share/nginx/app/public";
index index.php index.htm index.html;
charset utf-8;
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size 100m;
include hhvm.conf;
location ~ /\.ht {
deny all;
}
}
Note: I am also using CloudFlare.
When I visit example.com, I am redirected to https://example.com. Great.
But www.example.com redirects to https://www.example.com and the site does not load.
Visiting https://example.com works fine.
Is this a server configuration problem or a CloudFlare problem? How can it be fixed?
The problem was that I needed to add a DNS A record to allow www to point to my server's IP address. Then, nginx could redirect without a problem.

Nginx redirect http to https not working

I'm setting up an nginx server with an SSL.
The domain with the ssl is dev.cooknconnect.com
I want to redirect all requests from: http://domain.com to https://domain.com
I have the following server blocks setup currently:
server {
listen 1.2.3.4:80 default;
server_name domain.com;
server_tokens off;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name domain.com www.domain.com;
keepalive_timeout 70;
server_tokens off;
ssl on;
ssl_certificate /etc/ssl/certs/certificate.crt;
ssl_certificate_key /etc/ssl/private/certificate.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://domain.com;
}
}
This currently does not work, but I don't understand why not.Can anyone find any mistake in my config.If not please suggest some way to track or debugging the same.
You could use this and it should work normally
server {
listen 80;
server_name domain.com;
server_tokens off;
return 301 https://$http_host$request_uri;
}
keep in mind that https://dev.yourdomain.com won't work, because your certificate was issued only for the root domain, the https for the dev subdomain will show the yellow certificate warning.
This works fine for me:
server{
listen 80;
server_name *.example.com;
rewrite ^ https://$host$request_uri? permanent;
}
This will permanently rewrite http to https for the domain itself and sub-domains to https mode.

Nginx redirect http://www and naked http/https to https://www

I would like to redirect all traffic from the following domains:
http://domain.com
http://www.domain.com
https://domain.com
to
https://www.domain.com
I have a SSL certificate for the above domain. It hosts a Rails app, served by Passenger.
To accomplish the naked domain redirect, I've set up an URL redirect in my DNSimple account:
URL domain.com 3600 https://www.domain.com
My server blocks are as follows (Inspired by Nginx no-www to www and www to no-www amongst others):
server {
listen 80;
listen 443;
server_name domain.com;
ssl on;
ssl_certificate /etc/ssl/domain-ssl.crt;
ssl_certificate_key /etc/ssl/domain.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
server_tokens off;
access_log /dev/null;
error_log /dev/null;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.domain.com;
root /home/deploy/app/current/public;
passenger_enabled on;
passenger_app_env production;
passenger_set_cgi_param HTTP_X_FORWARDED_PROTO https;
ssl on;
ssl_certificate /etc/ssl/domain-ssl.crt;
ssl_certificate_key /etc/ssl/domain.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}
passenger_pre_start https://www.domain.com;
What works:
naked domain gets redirected to secure https://www
http://www domain gets redirected to secure https://www
https:// www works
What does't:
Naked https:// doesn't work, browsers throws a server not found
Basically I want to redirect all traffic to the secure https://www.domain.com. What am I missing here?
If you don't have a certificate for domain.com redirecting from https://domain.com to https://www.domain.com will not work, because before the browser gets the redirect it has to successfully establish the SSL connection (https is http inside SSL) and this fails because the certificate does not match.
I had similar kind of scenario and this is how I solved the redirection from
https://domain.com -----> https://www.domain.com
server {
listen 443;
server_name domain.com;
if ($host = domain.com) {
rewrite ^(.*) https://www.domain.com:443$request_uri? permanent;
}
Hope this helps!
Using if condition in nginx
Directive if has problems when used in location context, in some cases it doesn't do what you expect but something completely different instead. In some cases it even segfaults. It's generally a good idea to avoid it if possible. The only 100% safe things which may be done inside if in location context are: return ...; rewrite ... last;
Following will help.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
====>>> Your site configuratioin Goes here <<======
}
Provide a specific server block for the naked domain along with a general default. The more-specific ones will be used first.
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
return 301 https://www.$host$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
# omitting the rest for https://www.example.com
}
I use Let's Encrypt for my certificates so something like the following for default_server prevents redirecting the ACME challenges (note the second wildcard server_name for handling all https://*.example.com which don't have their own server block).
# omit server_name example.com block, same as above
server {
listen 80 default_server;
listen [::]:80 default_server;
location ~ ^/\.well-known/acme-challenge {
# LetsEncrypt
add_header Content-Type text/plain;
expires 0;
alias /var/www/html/acme/$host;
break;
}
location ~ ^/(?!\.well-known/acme-challenge) {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.example.com;
# omitting the rest for https://*.example.com
}
Setup certificates for the naked example.com, www.example.com and any others:
sudo certbot certonly --manual -d example.com -d www.example.com -d abc.example.com