azad education azad education

The next-generation blog, news about technology.

India (HQ)

[email protected]

how to remove .php .html .htm extension using htaccess

azad education
Azad Education Dec 06, 2022 · 6 min read

What is htaccess file and benefits?

Htaccess is a configuration file of apache which is used to make changes in the configuration on a directory basis. Htaccess file is used to do changes in functions and features of the apache server. Htaccess is used to rewrite the URL. It is used to make site address protected. Also to restrict IP addresses so on particular IP address site will not be opened.

  • A settings file for the server
  • Cannot be accessed by end-user
  • There is no need to reboot the server, changes work immediately
  • It might serve as a bridge between your code and server

htaccess file control number of way that a website  can be accessed, blocked  or redirects.htaccess file using some rewrite rule. These rewrites are made possible by Apache mode rewrite module.

for more tutorials https://www.youtube.com/watch?v=ojgBXMh3sas 

When a .htaccess file is placed in a directory which is in turn 'loaded via the Apache Web Server', then the .htaccess file is detected and executed by the Apache Web Server software.

These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.

These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

Whenever any request is sent to the server it always passes through .htaccess file. There are some rules are defined to instruct the working.

What we can do from htaccess.

  • URL rewriting
  • Custom error pages
  • Caching
  • Redirections
  • Blocking ip's

Below are some usage of htaccess files in server:

1) AUTHORIZATION, AUTHENTICATION: .htaccess files are often used to specify the security restrictions for the particular directory, hence the filename "access". The .htaccess file is often accompanied by an .htpasswd file which stores valid usernames and their passwords.

2) CUSTOMIZED ERROR RESPONSES: Changing the page that is shown when a server-side error occurs, for example HTTP 404 Not Found. Example : ErrorDocument 404 /notfound.html

3) REWRITING URLS: Servers often use .htaccess to rewrite "ugly" URLs to shorter and prettier ones.

4) CACHE CONTROL: .htaccess files allow a server to control User agent caching used by web browsers to reduce bandwidth usage, server load, and perceived lag.

How to use?

Rewrite Rule Example 

Rewrite contest http://example.com/node/123 [L,R]

Request goes to this url:-https://azadeducation.in/about-us 

Where to store htaccess file in website?

.htaccess file create in directory /var/www/html/.htaccess

Htaccess code website error page's

ErrorDocument 400 /400.php
ErrorDocument 401 /400.php
ErrorDocument 403 /400.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

Remove php pages extention from htaccess

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Website old url convert into full seo url from htaccess code.

RewriteEngine On

#old url => www.example.com/product?id=100

RewriteRule    ^product/([0-9]+)/([0-9a-zA-Z_-]+) product.php?id=$1&id=$1&productUrl=$2 [NC,L]

#new url =>  www.example.com/product/id/100

Website all pages redirect on one page.

RewriteCond %{REQUEST_URI} !/main-page.php$ 
RewriteRule $ main-page.php/ [R=302,L] 

Website set cache policy for website improvement.

# BEGIN EXPIRES
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

How to enable gzip for website improve performance.

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css
    AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css   application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

 Redirect all pages of my domain from http to https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Subscribe To Our YouTube Channel To Unlock Download Button
Download Subscribe
avatar

Azad Mohammed

An editor at AzadEducation
View Articles

I'am Azad, These blogs, tech skills and programing news, I am sharing with my exprience. I have 4+ year experiece in the web development. Also we are learning investing ideas. I believe in "big mind big dream", that will convert in smart mind. You can follow me on Linkedin profile.

Categories
C#
10
Java Script
8
SEO
4
Quick Hack Solution Tips
4

Subscribe to our mailing list!

We don't spam