First, verify that the mod_rewrite module is installed. Then, be careful to understand how it works, many people get it backwards.
- Redirect All Requests To Index.php Using .htaccess
- Get characters after last / in url
- Create Base URL and include PHP file with Base URL in Core PHP
Just add .htaccess file to the root folder of your site.
Now I’ll show you complete flow of website URL withour file extension .php, .html
.htaccess
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # Enable Rewriting RewriteEngine on # Rewrite user URLs # Input: user/NAME/ # Output: user.php?id=NAME #RewriteRule ^about/(\w+)/?$ about.php?id=$1 #RewriteRule ^about/?$ about.php #RewriteRule ^vipul-rai/?$ about.php?id=$1 #RewriteRule (.*) index.php [L] RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . index.php [L] ErrorDocument 404 /404.php |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <table width="100%" border="0" cellspacing="10" cellpadding="0"> <tr> <td width="50%"><a href="home">Home</a></td> <td width="50%"><a href="about-us">About Us</a></td> </tr> </table> <?php include "page-controller.php"; ?> |
page-controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php $cURL=$_SERVER['REQUEST_URI']; preg_match("/[^\/]+$/", "$cURL", $matches3); if(isset($matches3[0])) { $lastURL = $matches3[0]; } else { $lastURL =""; } if($lastURL!="") { switch ($lastURL) { case "": include"pages/home.php"; break; case "home": include"pages/home.php"; break; case "about-us": include"pages/about-us.php"; break; default: echo"page not found"; } } else { include"module/pages/home.php"; } ?> |
pages/about-us.php
Create a folder ‘pages’ and save a file “home.php / about-us.php”. this is your landing page, you can do what you want to do. 🙂