We are aware of a potentially service impacting issue. Learn more

How to write .htacess File Tips Print

  • #How to write .htacess File Tips
  • 43

How to write .htacess File  Tips

 It is very important file. With .htaccess file, you can  easily configure and redirect Apache Web Server file system. After this aricle you can create friendly URLs, sub domain directory re-directions and many more.

Open any text editor application and file save as with .htaccess name



Disable directory Listing

If you want to disable folder files listing, include following code.

1
Options All -Indexes

Error Pages

Here error page is redirecting to error.html.

1
2
3
4
ErrorDocument 400 http://www.yourwebsite.com/error.html
ErrorDocument 401 http://www.yourwebsite.com/error.html
ErrorDocument 404 http://www.yourwebsite.com/error.html
ErrorDocument 500 "Sorry, our script crashed. Oh dear"

RewriteEngine On

RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.

1
RewriteEngine on

Hide .php extension with URL Rewriting

For example if we want to project like Twitter API URLs (Note: Twitter API Developed in Ruby on Rails)

1
2
RewriteEngine on
RewriteRule ^(.*)\$ $1.php

We can Rewrite index.php into index.html,index.asp,index.sri also … Below code for index.php to index.html

1
2
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php

If you want .asp extension just replace html to asp

Redirecting www URL to non www URL

If you type www.yourwebsite.com in browser it will be redirected to yourwebsite.com .
Add this Following Code:

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourwebsite.com
RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]

Conversely, redirecting yourwebsite.com to www.yourwebsite.com

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection

Sub domain redirection mapping to folder.
Here www.yourwebsite.com is connecting to website_folder folder.
And subdomain.yourwebsite.com is connecting to subdomain_folder folder.

1
2
3
4
5
6
7
8
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

Friendly URL with parameters

Original URL : yourwebsite.com/followers.php?id=abc123
to
Rewriting URL : yourwebsite.com/abc123/followers

1
2
3
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/followers$ followers.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/followers/$ following.php?id=$1

Htaccess File Inside The Folder

Original URL : www.yourwebsite.com/blog/index.php?id=abc123
to
Friendly URL : blog.yourwebsite.com/abc123

First .htaccess file
This code redirects sub domain blog.yourwebsite.com pointing to blog folder.

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1

Second .htaccess file
This file inside the blog folder. Contains single parameter URL rewriting code.

1
2
3
4
5
RewriteEngine On
RewriteBase /blog/
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1

Was this answer helpful?

« Back

Powered by WHMCompleteSolution