How to Make User Friendly Url With .Htaccess And Php?

3 minutes read

To make user-friendly URLs with .htaccess and PHP, you can use .htaccess to rewrite the URLs and PHP to process the rewritten URLs.


First, you need to create a .htaccess file in the root of your website directory. In this .htaccess file, you can use the RewriteEngine on directive to enable URL rewriting.


Next, you can use the RewriteRule directive to define how URLs should be rewritten. For example, you can rewrite a URL like example.com/index.php?page=about to example.com/about.


In your PHP script, you can then process the rewritten URL to determine which content to display. You can use variables like $_GET['page'] or $_SERVER['REQUEST_URI'] to parse the URL and fetch the corresponding content from your database or files.


By combining .htaccess rewriting with PHP processing, you can create user-friendly URLs that are easier to read and remember for both users and search engines.


What is the role of PHP in creating user-friendly URLs?

PHP plays a critical role in creating user-friendly URLs by allowing developers to dynamically generate and customize URLs based on user interactions or parameters. With PHP, developers can easily modify URLs to make them more descriptive, readable, and SEO-friendly. Additionally, PHP allows for the implementation of URL rewriting techniques, such as using mod_rewrite in Apache, to create clean and structured URLs that enhance the user experience. This, in turn, helps improve website usability and makes it easier for users to navigate and share content.


How to create user-friendly URLs using .htaccess and PHP?

  1. Enable mod_rewrite in your Apache server by adding the following line to your configuration file:
1
RewriteEngine On


  1. Create a .htaccess file in the root directory of your website and add the following code:
1
2
3
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]


  1. Create a PHP script (e.g. index.php) that will handle the requests and parse the URL parameters:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

$url = isset($_GET['url']) ? $_GET['url'] : '/';
$urlParts = explode('/', $url);
$controller = $urlParts[0];
$action = isset($urlParts[1]) ? $urlParts[1] : 'index';

// Include the appropriate controller based on the URL
include 'controllers/' . $controller . '.php';

// Call the action method in the controller
$controllerObj = new $controller();
$controllerObj->$action();

?>


  1. Create separate controller files for each page or functionality of your website, and define the action methods within them:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// controllers/home.php
class Home {
    public function index() {
        echo 'Welcome to the homepage';
    }
}

// controllers/about.php
class About {
    public function index() {
        echo 'About Us';
    }
}


  1. In your HTML links, use user-friendly URLs that map to the controller and action:
1
2
<a href="/home">Home</a>
<a href="/about">About Us</a>


By following these steps, you can create user-friendly URLs that are easy to understand and navigate for users, while still maintaining the functionality and structure of your website.


How to rewrite URLs using .htaccess?

To rewrite URLs using .htaccess, you can create rewrite rules in the .htaccess file in the root directory of your website. Here are some common examples of how you can rewrite URLs:

  1. Redirecting non-www URLs to www URLs:
1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


  1. Removing file extensions from URLs:
1
2
3
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]


  1. Redirecting specific URLs to new URLs:
1
2
RewriteEngine On
RewriteRule ^old-url$ /new-url [R=301,L]


  1. Rewriting query string URLs:
1
2
RewriteEngine On
RewriteRule ^category/([^/]+)/?$ products.php?category=$1 [L]


Make sure to test your rewrite rules after adding them to the .htaccess file to ensure they are working as expected. Also, be cautious when modifying your .htaccess file as incorrect configurations can cause issues with your website.

Facebook Twitter LinkedIn

Related Posts:

In order to define the base URL in a .htaccess file, you can use the RewriteBase directive. This directive is used to set the base URL for all URL-path references within a .htaccess file.For example, if your website&#39;s base URL is www.example.com, you would...
To shorten a URL address using .htaccess, you can use URL rewriting rules to redirect requests from long URLs to shorter, more user-friendly ones. This can be done by creating rewrite rules in the .htaccess file in the root directory of your website.First, you...
To modify a URL using .htaccess, you can use mod_rewrite module in Apache. This allows you to transform or redirect URLs based on certain criteria specified in the .htaccess file. You can create rewrite rules using regular expressions to match specific pattern...
To rewrite index.php to appear as part of the URL using .htaccess, you can use mod_rewrite rules in the .htaccess file. This process involves creating rules that redirect requests for the index.php file to a specific URL structure without displaying the actual...
To remove the question mark and the forward slash from the URL using .htaccess, you can use the RewriteRule directive in your .htaccess file. You can create a rule that matches the specific pattern of the URL you want to modify and then rewrite it without the ...