Lucas Callado | Blog    About    Archive

.htaccess

*\** Rewrite folder name to a filename ***

Admin url is folder/cms-admin/index.php and access should be folder/admin

RewriteRule ^admin cms-admin/index.php [R]

*\** All requests to page.html will be sent to page.php ***

This will rewrite any filename with .html to .php

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [NC]

[NC] means “No Case Sensitive”;

*\** All files of .html to .php ***

RewriteRule ^(.+)\.html$ http://site.com/$1.php

*\** Rewrite www to non www url ***

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

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

*\** Rewrite non www to www ***

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

*\** Rewrite non www to www with https ***

All non www url will be re-written to www with prefix https

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site.com/$1 [R,L]

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

*\** Redirect http to https on one page only ***

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^contact/?$ https://secure.example.com/contact/ [R=301,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folderName
RewriteRule ^(.*)$ https://www.site.com/folderName/$1 [R,L]

*\** Rewrite file links ***

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

Final result:

site.com/files/foo.zip

*\** Rewrite http to https for only one folder ***

RewriteEngine on
DirectoryIndex index.php index.html
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site.com/test/$1 [R,L]