IMPORTANT HTACCESS FILES TUTORIAL AND TIPS

htaccess Files Tutorial
.htaccess Files Tutorial

You can get simple and understandable steps to easily configure .htaccess file. Before we move into the code that can be done using .htaccess , let’s have a brief look .htaccess file. htaccess redirect rule to redirect your site.

What is .htaccess file?

Htaccess is a configuration file used on web servers. Htaccess file used for running the Apache Web Server software to enable/disable the functionality of the Apache Web Server, .htaccess file is loaded into a directory of Apache Web Server software. Also, the functionality of .htaccess file directories can be accessed by the web servers to perform its operations.

There are N number of functionalities can be done through .htaccess files, which are explained below with their coding part. Let’s familiar the most commonly used and important functionalities of .htaccess file system.
htaccess is a powerful Apache configuration

How to create .htaccess File?

Let’s begin with the creation of .htaccess file.

  • Open any text editor -> Click on “Save As” -> Replace extension .txt to .htaccess
  • We need to enable mod_rewrite extension in php.ini file in Apache Web Server configurations.

Now we created our own .htaccess file on our platform. Let’s start with important parts of the .htaccess file system.

Why Can’t I Find My .htaccess File?

On most of the file systems, filenames that begin with a dot ( . ) are hidden files. That means they are not typically visible by default for the user.

But you can easily make it visible. On your FTP client or File Manager should have a setting for “show hidden files.” This will be in different places in different software, but is usually in “Preferences”, “Settings”, or “Folder Options” menu. Sometime you’ll find it in the “View” menu.

How to turn on the Rewrite Engine?

You can turn on/off the rewrite Engine of the .htaccess file using the below code.

What is URL?

URL stands for Uniform Resource Locator. Also known as a web address. URL used to specify addresses on the webpage WWW (World Wide Web). it is a network Identifier. it should be unique and easy to identify.

Below are some code parameters to define your own URL for the search engine.

Your URL can have parameters like alphanumeric, only alphabets or only numeric.

[a-zA-Z0-9_-] – It will allow you to enter alphanumeric characters.
[0-9] – This will allows you to enter numeric characters.
[A-Z] – This will allows you to enter alphabet characters.
You can design your own parameter block for your requirements.

What is the Profile URL?

Simply profile URL defines your profile, which helps to identify you in the community. The usual URL will have special characters in between proper names.

You can use below code to rewrite your URL from

https://phpadvices.com/profile.php?username=vijay
To
https://phpadvices.com/vijay

What is the Message URL?

Message URL is the same as profile URL, instead of profile.php, just replace it with messages.php And then change the username with your page message. Below is an example.


https://phpadvices.com/messages.php?messages=phplearning
To
https://phpadvices.com/messages/phplearning

Instead of Message, you can provide any name like description, domain, friends, abc etc.,

How to create multiple parameters in the URL?

You can use multiple parameters in a single URL. Only by modifying the parameter block accordingly as mentioned earlier.

https://phpadvices.com/messages.php?messages=phplearning&page=1
To
https://phpadvices.com/messages/phplearning/1
OR
https://phpadvices.com/messages/phplearning/introduction
OR
https://phpadvices.com/messages/phplearning/page1

Please add htaccess rule like below

How to hide the file extension using htaccess?

You can hide your file extensions like .html or .php etc., Use the below code to hide your extension of URL

How to redirect 404 error page in PHP using htaccess

You can redirect your webpage with error codes. For that, you have to define the error code to the appropriate webpage links in the .htaccess file. Below is the code you have to provide with respect to your requirement.

The syntax looks like below.
ErrorDocument Error Code Page Name
ErrorDocument 404 /404.php

  • 202 Accepted
  • 301 Moved Permanently
  • 305 Use Proxy
  • 307 Temporary Redirect
  • 400 Bad Request
  • 401 Authorization Required
  • 402 Payment Required
  • 403 Forbidden
  • 404 Not Found
  • 407 Proxy Authentication Required
  • 408 Request Time-out
  • 500 Internal Server Error
  • 501 Method Not Implemented
  • 502 Bad Gateway
  • 503 Service Temporarily Unavailable
  • 504 Gateway Time-out
  • 505 HTTP Version Not Supported

Redirect Url to another URL htaccess

The below code can be used either redirecting the old page to a new page name or old domain to the new domain name.

You can redirect your webpage from to oldwebpagename.com to www.newwebpagename.com
Also, You can redirect your webpage from www.oldwebpagename.com to www.newwebpagename.com
Please check the below example.

Redirect to subdomain htaccess

You can redirect your subdomain page to a specific main or subfolder page as per your requirement.
Main Folder :
For example https://www. yourwebsitepagename.com is connecting to the “main_folder” folder.

Sub Folder :
For example https://www. subdomain.yourwebsitepagename.com is connecting to sub_folder folder.

Avoid directory listing htaccess

This can be done by disabling the directories of your webpage, using the following code.

Forcefully redirect to https using htaccess

If you have installed an SSL certificate to your server, you can force all visitors to your site to use HTTPS to ensure your website is secure. You can use the below code to forcefully redirect to https

This can be done by disabling the directories of your webpage, using the following code.

Once you did this, You can see the access forbidden page when you access the directory from URL.

Here you go…!! We have learnt some important .htaccess file system.

You can now start practicing on .htaccess!!

Hope it helped you in an easy way to write .htaccess files with more understanding. Please let us know in the comment section if you have any concerns.

Also, You Can Learn

Apache Web Server  – Check out our article, How to Install Apache on CentOS 7.
PHP  – Check out our article, How to Install PHP on CentOS.
MySQL  – Check out our article Install MySQL on CentOS 7.

You can also share with your friends, who need to learn Htaccess. Will meet you in my upcoming articles.