http://Check-these.info/RewriteRule.html


"RewriteRule" defines URL to filepath transformation.

See mod_rewrite.html, mod_rewrite-basic.html for more info.

Example: Generic .htaccess Method for sub/pointed domains

Commonly used code

They are "Generic code" and no editing required.

Note 1: You may not need Options FollowSymlinks as it's commonly set by serverwide config file, already.
Note 2: You need RewriteEngine On only once in the .htaccess file.


If you have 'www' dropping from the URL or asked twice for the password, this is the fix.
It happens when you enter the URL of a directory without trailing slash and your domain name contains 'www'.
ex. http://www.example.com/subdir <== No slash at the end.
Generally, you should ALWAYS put a slash after a directory name.
But some robots and links don't follow this practice, and you may want to use this fix.

In short, you only need this code if you use 'www.' for your domain name.
I prefer URL without 'www', these days, as it's shorter and easier.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
  
If you want to cover both http and https:
Options +FollowSymlinks
RewriteEngine On
RewriteCond s%{HTTPS} ^((s)on|s.*)$ [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

Note: These codes are very efficient compared to the code with '-d' check, 
but they won't work with directories that have a dot (period) in it. 
(ex. /a_directory.name/)


Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want to cover both http and https:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..*//((s)on|s.*) [NC]
RewriteRule ^ http%3://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

It may look complicated, but this code reduce the wasteful redirect to your site.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.)|.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(#)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.))$ [NC]
RewriteRule ^ http://%6%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]

If you want to cover both http and https:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www.//s%{HTTPS} ^/+(.+/)?[^.]*[^/]\\(/)(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.)|.*)//((s)on|s.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www.//s%{HTTPS} ^(/)?(/)?(([^.]{4,}|[^w.]?[^.][^w.]?[^.]?[^w.]?)\..+/(www\.))//((s)on|s.*)$ [NC]
RewriteRule ^ http%7://%5%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]


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

If you want to cover both http and https:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]


Questionable color of this page is dictated by blueberry cream cake, my favorite dessert.

This page is http://Check-these.info/RewriteRule.html