How to make pretty URL in PHP using .htaccess

Hello Everyone,

Here in this tutorial, you will learn How to make a pretty URL in PHP using .htaccess. and the Htaccess file tutorial and tips. using htaccess you can configure and redirect the apache web server file system. if you are using an Nginx server htaccess won’t work in your server, you have to use Nginx config for that. This tutorial will teach you how to create pretty URLs( friendly URLs) with your website.

.htaccess file will be in hidden format please turn on settings to show hidden files to view this file

How to create a .htaccess file

Open your text editor and save the file with the .htaccess extension (yes, you don’t need to name it anything .. its only needed the extension )

How to Disable Directory Listing using .htaccess

if you want to disable showing all files in the folder include the following code into your htaccess file

# Disable Directory Browsing
Options All -Indexes

 

How to Do Domain Redirection using .htaccess

here is the htaccess code for redirect your yourwebsite.com to www.yourwebsite.com

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

 

Okay, let’s talk about Friendly URLs now – Friendly URLs knows as Pretty URLs will help in search engine rankings.

let’s make one for our blog

How to make Pretty/ Friendly Url using htaccess

Think your URL is https://website.com/blog.php?link=my-first-post      Looks not great right. so let’s make it something like https://website.com/my-first-post this , Yes looks great. we can make it with 2 lines of code in htaccess  and the code the following 

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog.php?link=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog.php?link=$1

 

How to Hide File Extension using htaccess

Now we are going to hide some of our file extensions with htaccess.

want to know how? we are going to hide .html from the end  https://website.com/about.php to https://website.com/about

wants to know how ? here is the code

RewriteEngine On
RewriteRule ^([^/.]+)/?$ $1.php  #for php files

you can use the same for HTML files too

RewriteEngine On
RewriteRule ^([^/.]+)/?$ $1.html

Hope this helps.

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

Drag and Drop with Swapy and PHP

Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]

September 17, 2024

How To Generate A PDF from HTML in Laravel 11

Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]

June 22, 2024

How to get the next value of an array and loop it in the array in PHP

Do you know How to get the next value of an array and loop it in the array in PHP […]

July 28, 2023

New Open Source CRM for project Management and Invoicing

I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]

December 31, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now