File .htaccess

The .htaccess file explained and illustrated

Some important things first:

In WWW-domain setting you need to check the support of CGI-scripts.

Examples

1. Redirect from http://www to http:// :

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

2. Redirect from http://www to http:// and redirect to mobile version (e.g. - m.):

						RewriteEngine on
						RewriteCond %{HTTP_HOST} ^www.site.com [NC]
						RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]
						
						RewriteCond %{HTTP_USER_AGENT} (?i:midp|samsung|nokia|j2me|avant|docomo|novarra|palmos|palmsource|opwv|chtml|pda|mmp|blackberry|mib|symbian|wireless|nokia|hand|mobi|phone|cdm|upb|audio|SIE|SEC|samsung|HTC|mot-|mitsu|sagem|sony|alcatel|lg|eric|vx|NEC|philips|mmm|xx|panasonic|sharp|wap|sch|rover|pocket|benq|java|pt|pg|vox|amoi|bird|compal|kg|voda|sany|kdd|dbt|sendo|sgh|gradi|jb|dddi|moto|iphone|android)
						RewriteRule ^.*$ http://m.site.com/ [R=301,L]
					  

3. Redirect to another domain:

						RewriteEngine on 
						RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
					  

4. Redirect from http://www and http:// to https:// :

						RewriteEngine On
						RewriteCond %{SERVER_PORT} !^443$ 
						RewriteCond %{HTTPS} off
						RewriteRule ^(.*)$ https://site.com/$1 [R=301,L]
						RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
						RewriteRule ^(.*)$ https://site.com/$1 [R=301,L]
						RewriteCond %{HTTPS_HOST} ^www.site.com$ [NC]
						RewriteRule ^(.*)$ https://site.com/$1 [R=301,L]
					  

Download sample .htaccess

Comments