A collection of useful .htaccess codes.
To work all of these, add the following line at the beginning to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
Options All -Indexes
Options All +Indexes
http://www.example.com/index.php to http://www.example.com
RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
http://www.example.com to https://www.example.com
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
http://example.com to http://www.example.com
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
http://example.com to http://www.example.com
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
http://www.example.com to http://example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
http://www.example.com to http://example.com
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
http://example.com to http://example.com/
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
http://example.com/ to http://example.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
http://example.com/page-1.html to http://example.com/page-2.html
Redirect 301 /page-1.html http://www.example.com/page-2.html
http://example.com/page 1.html or http://example.com/page%201.html to http://example.com/page-1.html
Redirect 301 "/page 1.html" http://www.example.com/page-1.html
Redirect 301 / http://newsite.com/
This way does it with links intact. That is `www.oldsite.com/some/crazy/link.html` will become `www.newsite.com/some/crazy/link.html`. This is extremely helpful when you are just “moving” a site to a new domain.
http://example.com/sample.php to http://example.com/sample
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
DirectoryIndex homepage.html
Replace homepage.html with the actual URL of the page you want to use as index and you are done.
AccessFileName htac.cess
You also need to update any entries in the file itself or everywhere .htaccess is mentioned, otherwise you will be getting lots of errors.
This page wouldn’t have such a long without