howto drupal 8 URL 301 redirects

4.5
(2)

You may want to redirect all traffic from http://example.com and http://www.example.com to https://example.com. You can do this by adding the code below to your server configuration file, i.e., the VirtualHost definitions:

VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

The use of RewriteRule would be appropriate if you don’t have access to the main server configuration file, and are obliged to perform this task in a .htaccess file instead:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com*
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

There are existing comments in .htaccess that explain how to redirect http://example.com to http://www.example.com (and vice versa), but this code here redirects both of those to https://example.com.

Similar Posts:

857

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Scroll to Top