htaccess

In this tutorial you will learn about the .htaccess file and the ability it has to improve your website. HostSG supports .htaccess files however as a customer you are responsible for what is in this file and how it changes your site.

You can create a .htaccess file on your local computer or on the server.

On your computer:

Windows: Using Notepad save the file as .htaccess

Mac OS X: Using TextEdit save the file as ".htaccess"
Note: If you save the file as .htaccess it will be hidden and you will not be able to see it in the Finder.
Using your preferred FTP client upload the file to the server. The .htaccess file will need to be in the folder where your site is located, typically the public_html folder.

On the server:

  1. Login to your cPanel Account
  2. Click the File Manager icon, located in the Files section.
  3. From the File Manger popup choose "Choose Hidden Files (dotfiles)."
  4. Note: If you do not see this popup you will need to click the "reset all interface settings" link at the bottom of the cPanel page.
  5. From the File Manager popup choose the folder you would like to open, most often the "Web Root (public_html/www)"
  6. Click the Go button.
  7. Note: If you are in the File Manager already you can add &showhidden=1 to the end of the URL.
  8. Click the New File icon to create a new file and name it .htaccess
  9. Alternative Index Files

 

Custom Error Pages

You can customize your own, personal error pages (for example when a file is not found) instead of using HostSG's error pages or not having an error page. This will make your site seem much more professional.

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html
For example if I had the file notfound.html in the root directory of my site and I wanted to use it for a 404 error I would use:
ErrorDocument 404 /notfound.html
If the error page is not in the root directory of your site, you can enter the path to the file:
ErrorDocument 500 /errorpages/500.html
These are some of the most common errors:

  • 401 - Authorization Required
  • 400 - Bad request
  • 403 - Forbidden
  • 500 - Internal Server Error
  • 404 - Wrong page


Stop a Directory Index From Being Shown

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

To prevent against this (without creating lots of new 'index' files, you can enter a command into your .htaccess file to stop the directory list from being shown:



# disable directory browsing


Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes

# enable directory browsing
Options All +Indexes

 



Deny/Allow Certain IP Addresses

To only allow people with specific IP addresses to access your site (for example, only allowing people using a particular network to get into a certain directory) or you may want to ban certain IP addresses (for example, keeping disruptive members out of your message boards).This will only work if you know the IP addresses you would like to ban.

Please keep in mind that most ISP's use dynamic IP addresses, so this is not always the best way to limit/grant access.

Block an IP Address



#Deny List


order allow,deny
deny from 123.123.123.123 #specify a specific address
deny from 123.123.123.123/30 #specify a subnet range
deny from 123.123.* #specify an IP address wildcard
allow from all



Allow only Certain IP Addresses



#Allow List


order deny,allow
allow from 123.123.123.123 #specify a specific address
allow from 123.123.123.123/30 #specify a subnet range
allow from 123.123.* #specify an IP address wildcard
deny from all




Note: This will still allow scripts to use the files in the directory.

Redirection

There is a tool in the cPanel that can create the Redirects for you, please see How to create a Redirect

Redirect from a specific file to a new file

Example:

 


Redirect /redirect_from.html http://www.newsite.com/folder/redirect_to.html


 


In the above example, a file in the root directory called redirect_example.html is redirected to the URL http://www.newsite.com/folder/redirect_example.html
If the old file were in a subdirectory then you could use:

 


/subdirectory/redirect_from.html



WildCard Redirect / Redirecting from one folder to a new folder

 


Redirect /redirect_from http://www.newsite.com/redirect_to



Now any request to your site below /olddirectory will be redirected to the new site, with the extra information in the URL added on, for example if someone typed in:

 


http://www.example.com/redirect_from/images/image.gif



They would be redirected to:

 


http://www.newsite.com/redirect_to/images/image.gif



Redirecting (URL Rewriting) with Joomla

To enable URL Rewriting in Joomla you will need to copy and paste the following code into your .htaccess file.

 


# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch





  • 209 Users Found This Useful
Was this answer helpful?

Related Articles

WordPress

WordPress is a free and open-source content management system (CMS) based on PHP and MySQL....

Uploading Website Files

Where to upload website files:If you want your website visitors to be able to view and access...

508 Resource Limit Reached

The error message "508 Resource Limit Is Reached" appears when your account is constantly...