Tektriks

Coding starts here

Simple HTACCESS Tutorials

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 /

General HTACCESS tips

  • Disable directory browsing

    
    Options All -Indexes	
    	
    
  • Enable directory browsing

    
    Options All +Indexes	
    	
    
  • Remove index.php from url (301 permanent redirect – SEO Friendly)

    http://www.example.com/index.php to http://www.example.com

    
    RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
    
    
  • Redirect http to https (301 permanent redirect – SEO Friendly)

    http://www.example.com to https://www.example.com

    
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    
  • Redirect non-www to www (301 permanent redirect – SEO Friendly)

    http://example.com to http://www.example.com

    
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
    
    
  • Redirect non-www to www – For any site (301 permanent redirect – SEO Friendly)

    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]
    
    
  • Redirect www to non-www (301 permanent redirect – SEO Friendly)

    http://www.example.com to http://example.com

    
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    
    
  • Redirect www to non-www – For any site (301 permanent redirect – SEO Friendly)

    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]
    
    
  • Add trailing slash at the end of URL (301 permanent redirect – SEO Friendly)

    http://example.com to http://example.com/

    
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
    
    
  • Trim trailing slash at the end of URL (301 permanent redirect – SEO Friendly)

    http://example.com/ to http://example.com

    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [R=301,L]
    
    
  • Redirect a Single Page (301 permanent redirect – SEO Friendly)

    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
    
    
  • Redirect a Single Page having Space or %20(ASCII character) in URL (301 permanent redirect – SEO Friendly)

    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 whole site (301 permanent redirect – SEO Friendly)

    
    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.

  • Remove php file extension from URL (301 permanent redirect – SEO Friendly)

    http://example.com/sample.php to http://example.com/sample

    
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^.]+)$ $1.php [NC,L]
    
    
  • Modify default index page to another

    
    DirectoryIndex homepage.html	
    	
    

    Replace homepage.html with the actual URL of the page you want to use as index and you are done.

  • Rename htaccess file

    
    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

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz