SSL and add-on domain

I have two small sites on cPanel shared hosting - main.co.uk and add-on.co.uk. This worked fine until recently, when I added an SSL certificate to main.co.uk and changed the .htaccess to redirect to the https version of the URL. I’ve just become aware that the add-on domain is now also redirecting to the https version of the main domain, which is not what I want.

I’m currently using this for the redirection:

#To redirect to https and www.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.main\.co.uk$
RewriteRule .? http://www.main.co.uk%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://www.main.co.uk%{REQUEST_URI} [R,L]

What do I need to do to stop add-on.co.uk redirecting to main.co.uk?

The easiest is to change RewriteCond %{HTTP_HOST} !^www\.main\.co.uk$.
Right now it matches anything that is not www.main.co.uk, but if I understand correctly it should only need to match main.co.uk. In that case it would be RewriteCond %{HTTP_HOST} ^main\.co.uk$

What remains then is that add-on.co.uk should probably also be rewritten to HTTPS I suppose, for that you can change RewriteRule .? https://www.main.co.uk%{REQUEST_URI} [R,L] to RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

So, all in all:

# Redirect main.co.uk to www.main.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^main\.co\.uk$
RewriteRule .? http://www.main.co.uk%{REQUEST_URI} [R=301,L]

# Redirect HTTP to HTTPS
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Note that I’ve also changed R in the second RewriteRule to R=301, as the default is 302.

1 Like

Thanks, @rpkamp. smile

No; the SSL certificate doesn’t cover it. (We will eventually get round to doing a major overhaul of the second site, at which point it will probably be moved to its own hosting.)

Try the following to test all the URLs and returned http response codes 200, 301, 302, 404, etc:

https://supiet2.tk

In that case you can leave the second block as it was. Except I would still replace R with R=301 :slight_smile:

1 Like

Wouldn’t a 302 be better if the domain is going to get its own hosting at some time?

The main site is working OK, but the add-on site is giving me an error. The correct URL appears in the address bar, but Firefox says:

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

and Chromium:

This page isn’t working

add-on.co.uk redirected you too many times.

ERR_TOO_MANY_REDIRECTS

(There are no cookies associated with either domain.)

I’m using

# Redirect main.co.uk to www.main.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^main\.co\.uk$
RewriteRule .? http://www.main.co.uk%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://www.main.co.uk%{REQUEST_URI} [R=301,L]

How have I screwed up? redface

Surely that would only apply if I were redirecting it temporarily to another domain? Both sites should appear under their respective domains.

Yes, sorry I misunderstood. I thought one was being redirected to t’other.

1 Like

The second part should not be:

RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://www.main.co.uk%{REQUEST_URI} [R=301,L]

but rather:

RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

However I don’t see how that would cause an infinite redirect :confused:

Can you post your entire .htaccess file please?

Ah, sorry. I misunderstood this bit:

I thought you meant to keep the second part from my original code.

Here’s the full thing. The only change from the previously-working version is the redirect to https.

# disable directory browsing
Options All -Indexes

# prevent folder listing
IndexIgnore *

<FilesMatch "\.(htaccess|htpasswd|ini|inc|phps|fla|psd|log|sh)$">
 Order Allow,Deny
 Deny from all
</FilesMatch>

#disable display_errors
php_flag display_errors off
php_flag log_errors on

# Enable the mod_expires module
ExpiresActive On
# Set expiration date to 1 month for all style sheets and images
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
# Set expiration date to 1 week for all HTML pages
ExpiresByType text/html "access plus 1 week"
ExpiresByType application/xhtml+xml "access plus 1 week"

AddDefaultCharset UTF-8

AddCharset UTF-8 .css

SetOutputFilter DEFLATE

<FilesMatch "\.(js|css|html|php)$">
SetOutputFilter DEFLATE
</FilesMatch>


# Redirect main.co.uk to www.main.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^main\.co\.uk$
RewriteRule .? http://www.main.co.uk%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect 301 /index.html /index.php
Redirect 301 /contact.html /contact.php
Redirect 301 /design.html /design.php
Redirect 301 /portfolio.html /portfolio.php
Redirect 301 /pricing.html /pricing.php
Redirect 301 /standards.html /index.php
Redirect 301 /standards.php /index.php

ErrorDocument 404 /404.php

Oh wait, you don’t have SSL on the add-on site, right? In that case we should prevent redirects to https for that. Like so:

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^www\.main\.co\.uk$
RewriteRule .? https://www.main.co.uk%{REQUEST_URI} [R=301,L]

That will only redirect www.main.co.uk from http to https, and leave addon.main.co.uk alone.

Success! weee

Many thanks for that. smile

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.