URL Rewriting .php to .html
In this article you will learn how to reright your website URL from php to html or any other extension you want.
- Benefits of mod_rewrite
- Get pretty URLs
- Shortener website URLs
- Hide website variables (more security)
- SEO, User-Friendly
- Check and enable Mod Rewrite
Options +FollowSymLinks RewriteEngine On
- Example Goal
we want to use mod_rewrite to change our server URLs from PHP to HTML.
Dynamic URL: http://www.yourwebsite.com/profile.php?user=khalil (before writing)
Static URL: http://www.yourwebsite.com/profile/khalil.html (after writing)
A. Edit .htaccess file
Let's assume that this is profile.php source code:
<?php $user=$_GET['user']; . . ?>
We can browse users profiles by query $user variable from the database like this:
http://www.yourwebsite.com/profile.php?user=khalil -> This will get khalil's user profile page.
The first step you should do is to edit your .htaccess file, so to rewright php to html in the above scenario :
1- Create .htaccess file in your public_html folder (if not exists)
2- Copy and paste this code
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^profile/([^/\.]+)/?.html$ profile.php?user=$1 RewriteRule ^profile?.html$ profile.php </IfModule>
First we enable mod_rewrite by : RewriteEngine On
create rule to rewrite website URLs from profile.php?user=$1 to profile/$1.html ($1= any text)
create rule to rewrite profile.php to profile.html profile?.html profile.php
Now you can check the new website URLs by opening valid user profile with the new HTML path example : http://www.yourwebsite.com/profile.php?user=khalil
B. Edit your PHP anchors links
Now you need to modify your PHP files source codes by chaning anchors from PHP to HTML.
for example one of your website anchores is
<a hred="profile.php?user=khalil"> khalil </a>
Change it to be :
<a hred="profile/khalil.html"> khalil </a>
Thats all, you are done. If you find this artice is usefull share it with your friends .