htaccess

What is .htaccess and how can you remove extension from url using htaccess

Page Visited: 1838
Read Time:4 Minute, 0 Second

Hello Guys htaccess is really useful and an important file to be included in our directory where your website files are located. Its main purpose is to remove extension and redirect visitors.

What is .htaccess?

.htaccess will remove any extension from url like .php, .html. It works only on server so you need apache server to run .htaccess file. When you run the server it detects the .htaccess file and configures it automatically.

It does not have any file name the extension .htaccess itself is the file name. It isn’t file.htaccess, it is simply .htaccess.

The .htaccess file affects the directory and the sun-directories where it is located. For example if there is one .htaccess file located in your root directory of yoursite.com, it would affect yoursite.com/content/, yoursite.com/content/images/, etc.

But you don’t want to rewrite the url in some directories then place a separate .htaccess file and that directory will follow that configuration.

With .htaccess you can:

  • Rewrite URLs
  • Specify your own Error Documents
  • Password protect a specific directory
  • Block users by IP
  • Redirect the user to different page

Rewrite URLs

To remove .php extension from your url yoursite.com/about.php to yoursite.com/about you have to add the following code in you .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you want to remove .html extension from your url yoursite.com/about.html to yoursite.com/about you have to add the following code in you .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

If you want to remove .htm extension from your url yoursite.com/about.htm to yoursite.com/about you have to add the following code in you .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.htm [NC,L]

Error Document

You can create your custom error page which is very useful. It allows you to display friendly error, explaining possible solutions and guiding the visitor back into your web site content. But if you use the basic error of the browser it will leave the visitor clueless and they will be lost.

Use this following code and create a 404.php file in your directory.

RewriteEngine on
#if requested resource isn't a file
# and isn't a directory
# then serve local error script 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .* 404.php [L] 

Password Protect a specific directory

Password protection system is used in apache web server and is one of the most important use of .htaccess. You can easily secure a directory of you website by using a username and password. The login procedure for these secure directories is handled automatically by the web browser using a pop-up login interface (you’ve probably seen these before). Passwords are also encrypted using one of the best encryption methods available which ensures login credentials are kept secure.

This is the .htaccess file:

AuthType Basic
AuthName "restricted area"
AuthUserFile /your site path /.htpasswd
require valid-user

This is the htpasswd file you can use this link to generate username and password

Redirect

It helps us to redirect our visitors from one link to another. For example if you have moved some of your content from old link to new link, you can redirect your visitors using .htaccess redirect. Redirect enables us to change the url

See below example I am redirecting http to https

RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://site.com/$ [NC]
RewriteRule ^(.*)$ https://site.com/$1 [L,R=301]

Here i am redirecting non www to www

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

Here i am redirecting www to non www

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

Block users by IP

It is really helpful feature of .htaccess. Apache server enables us to deny access to specific IP address or allow access. It is really useful when you want to limit certain parts of website to specific users like administration area.

This code is for denying certain IP
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

This code is for allowing certian IP
order allow,deny
allow from 255.0.0.1
deny from all

This above code will deny IP 255.0.0.0. The second line will block all the IP starting from 123.45.6.50 or 123.45.6.255 etc. It will allow 255.0.0.1.

.htaccess is one of the most important file in the process of website development. There is no need to learn this specifically, cause you can get ton of documentation and codes online.

Get all config here.

About Post Author

Girish

Hello Guys I am a website developer by profession but is always keen on learning new things. I have been investing in Mutual funds, stock market for the past few years because of which I have gained good knowledge. I started my entrepreneur journey in 2019 which lead me to learn more things as I am moving forward. I always love to share whatever I learn. Always had a craze for cars from my childhood, which inspired me to start this website.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.