What is .htaccess
.htaccess (where "ht" stands for hypertext) is a configuration file of the Apache web server. It lets you change server settings for one directory without editing the main configuration: enable or disable features, set up authentication, rewrite URLs, manage caching, change error pages (404, 500) and so on.
The settings apply to the directory where the file is located and to all its subdirectories. .htaccess is a plain text file, so you can edit it in any text editor. The file name must begin with a period.
Most hosting control panels and Apache servers support .htaccess, and some settings are easier to manage with it than in the main configuration. On a BlueVPS Linux VPS you have root access, so you can use both .htaccess and the main Apache configuration (httpd.conf or apache2.conf).
All examples below use mod_rewrite. Put RewriteEngine On once at the top of the file, and replace example.com with your domain.
Redirect from HTTP to HTTPS
Add these lines to the beginning of the .htaccess file in the root directory of your site (create the file if it does not exist):
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]If your site is behind a proxy or load balancer that sets the X-HTTPS header, use this variant:
RewriteEngine On
RewriteCond %{HTTP:X-HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]Redirect from HTTPS to HTTP
Add these lines to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]Redirect from One Page to Another
The simplest way is the Redirect directive:
Redirect 301 /testpage1/ https://example.com/testpage2/The same with mod_rewrite:
RewriteCond %{REQUEST_URI} ^/test/$
RewriteRule ^.*$ https://example.com/new-test/? [L,R=301]Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]Redirect from non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]Note: a condition like ^([^www].*)$, which you can find in many old guides, does not work correctly. [^www] means "any character except w", so the rule skips every domain that starts with the letter w.
Remove the Trailing Slash (the Whole Site)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ /$1 [L,R=301]Add a Trailing Slash (Often Set Automatically in CMS)
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L,R=301]The !\. condition skips addresses with a dot, so files such as style.css or image.png are not affected.
One Redirect (Not Two in a Row) to non-www with a Trailing Slash
These rules fix both the domain and the slash in a single redirect, which is better for SEO than two redirects in a chain:
# both problems: www and no slash
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1/ [L,R=301]
# only www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# only the missing slash
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L,R=301]One Redirect to www with a Trailing Slash
# both problems: no www and no slash
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1/ [L,R=301]
# only no www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
# only the missing slash
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L,R=301]One Redirect to www without a Trailing Slash
# both problems: no www and a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)/$ https://www.%{HTTP_HOST}/$1 [L,R=301]
# only no www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
# only the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ /$1 [L,R=301]One Redirect to non-www without a Trailing Slash
# both problems: www and a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)/$ https://%1/$1 [L,R=301]
# only www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# only the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ /$1 [L,R=301]Redirect example.com/index.php (without GET Parameters) to the Main Page
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ https://example.com/? [L,R=301]Remove index.php from Addresses with GET Parameters
For example, example.com/index.php?n=1 redirects to example.com/?n=1:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule ^(.*)$ https://example.com/ [L,R=301]The query string is kept automatically.
Redirect index.php, index.html and index.htm to the Directory (for Example, in Joomla)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.(php|html|htm)\ HTTP/
RewriteRule ^(.*)index\.(php|html|htm)$ https://example.com/$1 [L,R=301]Redirect a URL with GET Parameters (Dynamic URL) to a Static One
Option 1: an address with a GET parameter
RewriteCond %{QUERY_STRING} ^id=229$
RewriteRule ^.*$ /supermodel/? [L,R=301]Option 2: a specific page with a GET parameter
RewriteCond %{REQUEST_URI} ^/test/$
RewriteCond %{QUERY_STRING} ^id=229$
RewriteRule ^.*$ /supermodel/? [L,R=301]The question mark at the end of the target removes the original query string.
Redirect All Pages of One Domain to the Main Page of Another Domain
RewriteRule ^(.*)$ https://example.com/ [L,R=301]Redirect Every Page to the Same Address on Another Domain
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]Conclusion
These are the most common redirect scenarios. Test new rules on a copy of the site or with curl -I before applying them to production, because a wrong rule can create a redirect loop. If you have not found the right code for your case, open a ticket and the BlueVPS support team will help.
Blog