What is .htaccess

.htaccess, where ht is hypertext and access is access - is a configuration file used by the Apache webserver. This file can specify  a number of server settings such as enable/disable additional functionality and features that Apache has to offer. For example authorization and authentication, changing URL addresses, managing cache, changing the look of the 404, 500 error pages and so on.

It applies only to the given directory in which the file is located and its subdirectories. Like any configuration file, .htaccess is a text ASCII file and can be edited with any text editor. The .htaccess file must begin with the period character.

.htaccess files are used in almost all Apache web servers worldwide. There are a number of settings that cannot be done any other way than with .htaccess, and there are some that .htaccess makes much easier. Most hosting providers including BlueVPS support .htaccess.

The .htaccess files act as a subset of the server's global configuration file (such as httpd.conf) for the directory in which they reside or any subdirectories.

Let’s proceed then.

From http to https

To configure redirection, in the directory of your site, add to the beginning of the file .htaccess (if this file does not exist, then create, note the file starts with a dot), the following lines:

RewriteEngine On  

RewriteCond %{HTTPS} off  

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  


Alternative:

RewriteEngine On  

RewriteCond %{HTTP:X-HTTPS} !1  

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]  


From https to http

Go to your .htaccess file and these lines:

RewriteEngine on  

RewriteCond %{HTTPS} on  

RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI}  


Redirect from one page to another

Redirect 301 /testpage1/ http://abcdtestdomain.xyz/testpage2/  

RewriteCond %{REQUEST_URI} ^/test/$  

RewriteRule ^.*$ http://abcdtestdomain.xyz/new-test/? [R=301,L]  


Redirect from http://www.abcdtestdomain.xyz to http://abcdtestdomain.xyz 

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://%1/$1 [L,R=301]  


Redirect from http://abcdtestdomain.xyz to http://www.abcdtestdomain.xyz

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]  


Redirect from slash pages to non-slash pages (the whole site)

RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} ![^\/]$  

RewriteRule ^(.*)\/$ /$1 [R=301,L]  


Redirect from non-slash pages to slash (often set automatically in CMS systems)

RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteRule ^(.*[^\/])$ /$1/ [R=301,L]  


One (not two consecutive!) redirect to no www and with a slash at the end of the page address

RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://%1/$1/ [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} ![^\/]$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://%1/$1 [L,R=301]  

RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://%1/$1/ [L,R=301]  


One (not two consecutive!) redirect to c www and with a slash at the end of the page address

RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://www.%1/$1/ [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://www.%1/$1/ [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} ![^\/]$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]  


One (not two consecutive!) redirect to www and no slash at the end of the page address

RewriteCond %{REQUEST_URI} ^\/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} \/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)\/$ http://www.%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} \/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)\/$ http://www.%1/$1 [L,R=301]  


One (not two consecutive!) redirect to no www and no slash at the end of the page address

RewriteCond %{REQUEST_URI} ^\/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} \/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)\/$ http://%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} !\/$  

RewriteCond %{HTTP_HOST} ^www\.(.*)$  

RewriteRule ^(.*)$ http://%1/$1 [L,R=301]  


RewriteCond %{REQUEST_URI} !\?  

RewriteCond %{REQUEST_URI} !\&  

RewriteCond %{REQUEST_URI} !\=  

RewriteCond %{REQUEST_URI} !\.  

RewriteCond %{REQUEST_URI} \/$  

RewriteCond %{HTTP_HOST} ^([^www].*)$  

RewriteRule ^(.*)\/$ http://%1/$1 [L,R=301]  


Redirect only the address abcdtestdomain.xyz/index.php (without GET parameters) to the main mirror abcdtestdomain.xyz

RewriteCond %{REQUEST_URI} /index.php  

RewriteCond %{QUERY_STRING} ^\z  

RewriteRule ^(.*)$ http://abcdtestdomain.xyz/? [R=301,L]  


Redirect all addresses with index.php and GET parameters to pages with GET parameters only (cut in url index.php)

E.g. abcdtestdomain.xyz/index.php?n=1 to abcdtestdomain.xyz/?n=1

RewriteCond %{REQUEST_URI} /index.php  

RewriteRule ^(.*)$ http://abcdtestdomain.xyz/ [R=301,L]  


Redirect for index.php, index.html or index.htm (for example in Joomla), mass join

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html|htm)\ HTTP/  

RewriteRule ^(.*)index\.(php|html|htm)$ http://abcdtestdomain.xyz/$1 [R=301,L]  


Redirect url with GET parameters (dynamic URL) to static

First option (simple address with GET parameter)

RewriteCond %{QUERY_STRING} ^id=229  

RewriteRule ^.*$ /supermodel/? [R=301,L]  


Second option (from page and GET parameter)

RewriteCond %{REQUEST_URI} /test/  

RewriteCond %{QUERY_STRING} ^id=229  

RewriteRule ^.*$ /supermodel/? [R=301,L]  


Redirect all pages of one domain to the main page of another domain

RewriteCond %{REQUEST_URI} (.*)  

RewriteRule ^(.*)$ http://abcdtestdomain.xyz/ [L,R=301]  


Every page of one domain to the same address of another url

RewriteCond %{REQUEST_URI} (.*)  

RewriteRule ^(.*)$ http://abcdtestdomain.xyz/$1 [L,R=301] 


Conclusion

Phew! That’s quite a lot of different scenarios, isn’t it? We have covered the most popular ones. If you haven’t found the right code for your case, hit us with a ticket. BlueVPS always helps its customers. Take care!



Blog