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.

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

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

Get Free Access Now