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 integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

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

Get Free Access Now