How do I create redirects for my website? Print

  • 6

.htaccess files provide a way to make configuration changes in how directories/files route – this file is generally located within your /public_html/ directory. The .htaccess allows you to control certain redirects and to redirect certain requests – such as force users onto a HTTPS version of your site.

This article lists a few of the most common redirects with examples, but you may need to make changes based on the particular configuration you’re using. If what you’re after isn’t listed below, try a quick Google search for alternatives.

Rewrite a directory to be SSL access only
NOTE: This goes into a .htaccess file in the directory where you want to force SSL access.

RewriteEngine On
RewriteCond %{SERVER_PORT}!443
RewriteRule ^(.*)$ https://www.mywebsite.com/directory [R,L]

Rewrite a specific file in a directory to be SSL access only
NOTE: This goes into a .htaccess file in your ‘public_html’ directory.

RewriteEngine on
RewriteCond %{SERVER_PORT} !443
RewriteRule ^specificfile\.php$ https://www.mywebsite.com/directory/specificfile.php [R=301,L]

Rewrite the URL to force visitors to ‘www.’
NOTE: This goes into a .htaccess file in your ‘public_html’ directory.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]

Redirect any request for the root directory to forward to a subdirectory
NOTE: This goes into a .htaccess file in your ‘public_html’ directory.

RewriteEngine on
RewriteCond %{HTTP_HOST} www\.example\.com [NC]
RewriteCond $1 !^test/
RewriteRule ^(.*)$ http://www.example.com/test/$1 [L]

Redirect yourdomain.com to subdomain.yourdomain.com
NOTE: This goes into a .htaccess file in your ‘public_html’ directory.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^directory(.*) http://subdomain.yourdomain.com$1 [R=301,L]

Redirect ‘www’ to subdomain.yourdomain.com
NOTE: use in root or subfolder

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*) http://subdomain.yourdomain.com/$1 [R=301,L]

Was this answer helpful?

« Back