Url rewriting

URL rewriting is a technique used to modify the appearance of a URL to make it more user-friendly and SEO-friendly. It is often used to remove file extensions (such as .php or .html) or to clean up the URL structure of a website.

URL rewriting can be performed on the server-side using server-side scripting languages such as PHP or Apache mod_rewrite module. The process involves mapping the clean, user-friendly URLs to the actual URLs that the website uses to retrieve content from a server.

Here's an example of how to use Apache mod_rewrite to hide the .php extension in a URL.

htaccess example

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

This code will map requests for .php pages to their equivalent non-.php version and keep the same content intact. For example, a request for www.example.com/page will load the content from www.example.com/page.php.


Here's an example of how to use PHP to hide the .php extension in a URL.

Php Example

<?php
$page = $_GET['page'];
$pages = array('home', 'about', 'contact');

if (in_array($page, $pages)) {
  include($page . '.php');
} else {
  include('404.php');
}
?>

This code uses the $_GET superglobal to retrieve the value of the page parameter from the URL. It then checks if the value is in the $pages array and, if it is, includes the corresponding .php file. If the value is not in the array, it includes a 404 error page.

By using URL rewriting, you can improve the user experience and search engine optimization of a website by making URLs easier to read and remember.

kabeer

Edited  Feb 02, 03:01 am

Article tags

×
SignUp with Email
X