Redirecting or rewriting domains and URLs

 Leonie Vuilleumier

Part of any website are the domains and URLs that users type in their browsers in order to reach a web page. However, it can be challenging to manage should it need to be changed. Thankfully, there are URL redirects for that.

URL redirect (forwarding) allows you to forward your domain visitors to any URL of your choice, either to a new domain or a different website altogether.

In an Apache web server, use the following rewrite rules in your .htaccess file.

Rewrite the entire domain and pass the paths

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

Redirecting to www

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirecting from www (removing www)

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Tags

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.