Redirecting base domain to HTTPS, and sub-domains to HTTP - apache

I've been using Apache for a project, and have now decided to convert over to nginx for performance reasons as the project has grown quite a bit.
For this project, we serve our base domain and www sub-domain over HTTPS, but need to serve all other sub-domains over HTTP.
In Apache, I was able to accomplish this with the RewriteEngine doing the following:
RewriteEngine On
#Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [OR]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have my website about half setup with nginx, and this portion of the configuration has stumped me. How do I go about converting this to work with nginx?

I divided the answer in 4 virtual host. The first two solve the http to https redirect among the main domains. And the second part catches the subdomains and redirects from https to http:
# FIRST PART ---------------
# from http to https on main domains
server {
listen 80;
server_name domain www.domain;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name domain www.domain;
# blah, blah, https and virtualhost configuration
}
# SECOND PART ---------------
# from https to http and others subdomains
server {
listen 443 ssl;
server_name *.domain;
# blah, blah, https configuration
location / {
return 301 http://$host$request_uri
}
}
server {
listen 80;
server *.domain;
# virtual with http configuration
}

Related

Convert Apache RewriteCond to Nginx

I want to convert following Apache config to Nginx compatible config.
What it currently does is redirect all traffic which DOESN'T contain ^/nexus/content* in uri to HTTPS. That means even if I access http:// example.com/nexus only, it should go to HTTPS.
Bottom line is I want keep http:// example.com/nexus/content* in HTTP but http:// example.com/nexus should redirect to HTTPS. Hope the question is clear :)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/nexus/content*
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I tried following Nginx configs but didn't work so far. Appreciate any help.
if ($request_uri != ^/nexus/content*) {
rewrite (.*) https://example.com$request_uri;
}
And
location ~ ^/nexus/content* {
}
location / {
rewrite ^(.*)$ https://$http_host$request_uri redirect;
}
I'm not sure if this will work, but here goes:
server {
listen 80;
server_name example.com;
if ($request_uri !~ "^/nexus/content*") {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
add_header Strict-Transport-Security "max-age=31536000"; # MiTM Mitigation
// your normal rules here
}
Similar to yours, but it does a redirect and not a rewrite.

Force www with .htaccess behind varnish

I want to force www domain behind varnish and apache.
After installing varnish RewriteRule is not redirecting domain.com to www.domain.com
I have:
apache 2.4.7
varnish 3.0.5
several virtual hosts for multiple domains
My .htaccess directive was working very well until I installed varnish.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
I already tried to find for the solution but without success.
In my vcl I only configured this:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
All other code is commented.
Update your rules with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Not ideal, because it hard codes the domain, but should work :)
The rationale, is that HTTP_HOST contains port number and your redirect fails to handle it.
Alternatively, strip port number from HTTP_HOST variable using this in VCL:
sub vcl_recv {
# Normalize the header, remove the port
set req.http.host = regsub(req.http.host, ":[0-9]+", "");
}

DNS Rewrite Rules Apache To Ngnix Translation

I need you help i use to successfully migrate my site from Apache to Ngnix but i trying to convert some DNS rewrite rules and till now with no success . Can you please give me some translation help .
Rewrites that i wont to translate are :
RewriteCond %{HTTP_HOST} ^domain\.net\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.net\.mk$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\.mk\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.mk$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\.mk\/$1[R=301,L]
Thanks for you support , where do a need to put translated rewrites in my vhost template or in nginx.conf (single web site on VPS)
To redirect a number of domains to another domain use a separate server block, for example:
server {
listen 80;
server_name domain.net.mk www.domain.net.mk domain.mk www.domain.mk;
return 301 http://domain.com.mk$request_uri;
}
server {
listen 80;
server_name domain.com.mk;
...
}
See this document for more.

Apache 2.4: htaccess forwarding to https and removing www

I'm having a weird problem with an htaccess (Apache version 2.4). I am using the folliwing code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
to force the following redirects:
http should become https in general
any www. should be removed
As it's a TYPO3 installation, I'm having a domain record inserted at the entry page, but it does not have any redirects whatsoever. So any redirecting is/should be done by the .htaccess file.
However, here is the problem:
http + example.com gets forwarded to https + example.com
https + example.com stays the same
http + www + example.com gets forwarded to https + example.com
https + www + example.com stays the same, instead of forwarding to http + example.com
Does anybody have any idea what could be wrong with the code in the .htaccess file?
The problem is that if a user uses https://www.example.com/, he gets an annoying certificate issue warning. But as I understand the code, it should forward if either there is no https or if the domain is not "example.com", e.g. "www.example.com", so basically, it should work, no?
Thanks in advance,
Thomas

Redirect all HTTP requests (non-www & www, subdomains) to HTTPS

I want to be able to redirect all visitors to a website & its subdomains to HTTPS like the following:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
http://sub.example.com -> https://sub.example.com
http://www.sub.example.com -> https://sub.example.com
https://www.sub.example.com -> https://sub.example.com
Other redirects:
w (or ww, wwww, etc).example.com -> https://www.example.com
w (or ww, wwww, etc).sub.example.com -> https://sub.example.com
The redirects should be a cacheable HTTP 301 redirect. The configuration used is Apache 2.4.10 + mod-spdy, with Strict-Transport-Security (Plus a wildcard SSL cert).
Currently use the following in .htaccess:
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ "https://www.example.com%{REQUEST_URI}" [R=301,L]
RewriteCond %{SERVER_PORT} !^443
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^ "https://sub.example.com%{REQUEST_URI}" [R=301,L]
I've been using spdycheck.org to test this, it works fine for the main domain, with the subdomains I get the following message:
HTTP Traffic Allowed
This website can still be access via HTTP. HTTP
does not provide any widely implemented mechanism for using other
protocols like SPDY. Only traffic over SSL/TLS using the NPN Extension
can be optimized with SPDY. By allowing HTTP traffic, this website is
not even offering SPDY capable browsers the choice of using SPDY. If
possible, this website should redirect all http:// to https://,
ideally with a cachable HTTP 301 redirect.
What's a good way to accomplish this? I could modify httpd.conf if required also.
If you're doing it in your httpd.conf, put in something like:
<VirtualHost _default_:80>
RewriteEngine On
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NE,L,R=301]
</VirtualHost>
After all of your virtual hosts, and ensure that your other virtual hosts are port 443 only. This will make this redirect the global http vhost that will redirect each page individually to HTTPS and give a 301 redirect.