1

I want to redirect all incoming urls to my website to a single url for canonicalization purposes.

Following redirect conditions should meet

  1. http://example.com-> https://www.example.com
  2. http://www.example.com -> https://www.example.com
  3. https://example.com -> https://www.example.com
  4. www.example.com -> https://www.example.com

My current Rewrite rules written in httpd.conf look as follows

RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE] 

With the above rules I am able to achieve 1st,2nd and 4th rule but 3rd doesn't work for me. Any suggestions are welcome.

7
  • 1
    In my testing it is working for 3rd case also. Make sure to clear browser cache. Commented Jul 28, 2016 at 19:55
  • @anubhava 3rd case doesn't work for me even after deleting browser's cache. Commented Jul 29, 2016 at 4:53
  • @anubhava Can you please share the updated rules for me to test as well. Commented Jul 29, 2016 at 5:59
  • I tested exact same rule you've shown in question. Commented Jul 29, 2016 at 7:11
  • Unfortunately, 3rd redirect does not work for me.You can try it out using the actual domain nafa.in Commented Jul 29, 2016 at 18:26

2 Answers 2

1
+50

The definition for HTTPS and HTTP version of your site must (?) appear under different virtualhost entries (unless you are using some proxy in front of httpd).

This makes it easy to end up with differences between the configs in the two virtualhosts. Make sure the config for the redirect is the same in both virtualhosts for port 80 and port 443

The easiest way to do this is to make use of an Include statement and reference a common file in the two virtualhost definitions

For instance put these rewrites in a file base-rewrite.inc in the root config dorectory (/etc/apache2/base-rewrite.inc in debian/ubuntu; /etc/httpd/base-rewrite.inc in Redhat):

RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE] 

Then in your virtual hosts definition, Include this file:

<VirtualHost *:80> # HTTP default virtualhost ServerName www.example.com DocumentRoot /var/www/html/ Include base-rewrite.inc </VirtualHost> <VirtualHost *:443> # HTTPS default virtualhost ServerName www.example.com DocumentRoot /var/www/html/ # An example SSL config, but use your certificate files SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key Include base-rewrite.inc </VirtualHost> 

This will keep the rewrites consistent and make it easy to maintain and change them all at once

Sign up to request clarification or add additional context in comments.

1 Comment

I ran in to the solution myself, and this answer is very similar to my specific case: hosting at webfaction there are separate website configurations for HTTP and HTTPS, both needed the same .htaccess, as you suggest here.
0

If you're talking about canonicalization for SEO purposes then you should also add this meta tag to the head section of your web pages:

<link rel="canonical" href="https://www.example.com/" /> 

What this does is tell search engines that whatever url was used to reach this page, this is the preferred url and the one that should be indexed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.