What is an .htaccess file
An .htaccess file may be a simple ASCII file that you simply create with a text editor like Notepad or TextEdit. The file lets the server know what configuration changes to form on a per-directory basis.
Please note that .htaccess is that the full name of the file. It isn’t file.htaccess, it’s simply .htaccess.
.htaccess files affect the directory during which they’re placed in and every one children (sub-directories). for instance if there’s one .htaccess file located in your root directory of yoursite.com, it might affect yoursite.com/content/, yoursite.com/content/images/, then on…
It is important to recollect that this will be bypassed. If you don’t want certain .htaccess commands to affect a selected directory, place a replacement .htaccess file within the directory you don’t want to be affected with the changes, and take away the precise command(s) from the new file.
Features
With an .htaccess file you can:
Redirect the user to different page
Password protect a selected directory
Block users by IP
Preventing hot-linking of your images
Rewrite URLs
Specify your own Error Documents
In this tutorial we’ll be focusing only on rewriting URLs.
Removing Extensions
To remove the .php extension from a PHP file for instance yoursite.com/wallpaper.php to yoursite.com/wallpaper you’ve got to feature the subsequent code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you would like to get rid of the .html extension from an HTML file for instance yoursite.com/wallpaper.html to yoursite.com/wallpaper you merely need to change the last line from the code above, to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! you’ll now link pages inside the HTML document without having to feature the extension of the page. For example:
wallpaper
Adding a trailing slash at the top
I received many requests asking the way to add a trailing slash at the top, for example, yoursite.com/page/
Ignore the primary snippet and insert the code below. the primary four lines affect the removal of the extension and therefore the following, with the addition of the trailing slash and redirecting.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Link to the HTML or PHP file an equivalent way as shown above. Don’t forget to vary the code if you would like it applied to an HTML file rather than PHP.
For Example
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^location/([0-9a-zA-Z_]+) job-by-city.php?city=$1
RewriteRule ^category/([0-9a-zA-Z_]+) job-by-category.php?category=$1RewriteRule ^openings/([0-9a-zA-Z_]+)/([0-9a-zA-Z_]+) jobs-link.php?city=$1&category=$2
RewriteRule ^details/([0-9]+) job-details.php?jdid=$1
ErrorDocument 404 /404.php
RewriteRule ^jobs-in-delhi?$ jobs-in-delhi.php
Some people asked how you’ll remove the extension from both HTML and PHP files. I don’t have an answer for that. But, you’ll just change the extension of your HTML file from .html or .htm to .php and add the code for removing the .php extension.
Just use below code and modified according to your need. Note: this is very useful…