8 Simple Steps to Create (Pretty) Clean URL's with PHP & HTACCESS

8 Simple Steps to Create (Pretty) Clean URL’s with PHP & HTACCESS

Clean URL’s PHP, In this article, you will learn about creating Clean URL’s in PHP with apache htaccess. Here I’m using simple content management system application developed in PHP & MySQL.

You can read about the content management system from this article. After completing this article, you can create clean URLs in your projects without any doubt, it’s easy to follow but you need to know some basics about regular expressions. If you don’t know about regular expressions don’t worry, I’ve created a video course for you to learn easily with step by step guide and also complete source code provided in course.

1. About Clean URL’s PHP CMS Project Demo

In content management system I’ve created four types of pages, they are index.php that this home page used to display all the articles and also I’m using index.php file for displaying category based articles with URL query string that look like sitename.com/index.php?category=category-name, I’ll be converting this URL to clean URLs as sitename.com/category/category-name.

And the next page is single article page that is single.php and URL structure before clean URLs is sitename.com/single.php?url=article-name after making it as clean URL is sitename.com/article-name. And also you will learn about removing the .php extension from URLs.

You can see the final result in this video, after making the changes to this cms system. We will remove PHP extension from URL. After that, we will create clean URLs for single article page. Next, it’s category URLs, after that we will make category URLs as clean URLs. In category clean URL’s I’m displaying a category directory without a real directory in our file system. While making links like this you have to make links relative to the site root.

Creating PHP Clean URLs in Apache with HTACCESS

Are You Excited to Create Clean URLs on your own Projects?

I've good News for you! Created a Video Course with 30 minutes of Content...

In category pages we have a pagination, I’ll show you how we can convert them into clean URLs, pagination number after category name. Original pagination with category page looks like this sitename.com/index.php?category=category-name&page=2, we will make them as sitename.com/category/category-name/2

First of all, check whether mod_rewrite apache module is enabled or not? You can check it with by creating a new file with phpinfo function. Open this php file in the browser, after that search for mod_rewrite. You should see it as enabled.

If the mod_rewrite module is enabled then you can continue with the next steps.

2. How to Use mod_rewrite for Clean URL’s PHP

We can create clean URL’s With the help of .htaccess or httpd.conf. Only use httpd.conf file if you are hosting a single website on your server like VPS server. You can use the htaccess file if you are on the shared hosting server. Here I’ll be using htaccess.

We can Create Rewrite Rules in htaccess file to generate Clean URL’s, we will see about creating clean urls in next steps.

mod_rewrite module Helps modify URL’s at runtime. Allows to map URL’s without any file structure, we will see about category URL’s. By using mod_rewrite module we can remove query strings from URL’s.

And with mod_rewrite works based on regular expressions to create clean URL’s, You will also learn about regular expressions in next steps.

Steps to Use mod_rewrite

Create a file and save it as .htaccess, then turn on rewrite engine in .htaccess file by adding RewriteEngine on Then after that Create Rules to match URL requests for clean URL’s.

Example of htaccess Rule

Below is the example of htaccess rule.
RewriteRule URL Pattern Original URL [flags]

Every rule stars with Rewrite Rule followed by URL Pattern this is the regular expression. After that it’s Original URL, then flags.

Few Other things you should remember while working with htaccess and mod_rewrite

Every new rule should be on the separate line. After creating rewrite rules, test them.

And also check links & internal files linking (relative to site root), we will see about these while working on category URL’s. And apache takes care of internally loading correct files.

3. Regular Expression in 5 Minutes

For creating clean URL’s you should have some knowledge about Regular Expressions. By Reading this you will know about regular expressions, it’s like the crash course to regular expressions. You will learn what you need to know before writing clean URL’s.

First of all, What are Regular Expressions?
A sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text.
By default, Regular Expressions match text patterns without any special expressions.

Basics of Regular Expressions

As you know Regular Expressions text matches itself without any special expressions.
URL pattern in RewriteRule starts with caret symbol ^ for beginning and with dollar symbol $ for end of the pattern. An example would be like

In rewrite rules, we can separate multiple texts or page names with Pipe Symbol. And the example is ^contact|anotherpage|page2$

If you want to search for text, we can use above. In case if we will want to search for some pattern we will use these patterns.

Search for digits

To search for digits, we will use \d pattern
\d for digits matches 0-9

Search for words

To search for words, we will use \w pattern, it matches small a to z and capital A to Z, also 0 to 9 with the underscore.
\w matches a-z, A-Z, 0-9 and underscores

Few More Patterns for generating Clean URL’s PHP

. matches any character
\. matches dot or period
matches character group

In patterns, if we want to search for multiple words or multiple numbers we will use these repeat patterns.

* for 0 or more repeats
+ for 1 or more repeats
? 0 or Once Repeat

Each pattern consists of a pattern group, we can have up to 9 capturing groups in URL patterns.

We can retrieve the text from URL pattern to original URL with $1 or $2, you can see an example below.
^contact$ – text can be retrieved as $n that is $1 or $2.

4. Remove PHP Extension

To remove PHP extension from URL, we should use page name as URL pattern after that Original URL then finally flags. Here I’m using two flags that is NC for case insensitive. And L for specifying end of the rule or stop processing rule.

Rule starts with Rewrite rule, then URL Pattern.

URL Pattern starts with caret symbol ^ and ends with dollar symbol, inside these two symbols we will use URL pattern that is contact.

Next original URL that is $1.php, where $1 is the URL Pattern.

And the final URL looks like

5. Making Clean URL’s for Single Article Page

In single article page, I’m going to convert URL from sitename.com/single.php?url=article-name to sitename.com/article-name. To make this we need to change the URL pattern and the Orignal URL and everything is same. That is rules starts with RewriteRule and the flags.

In URL Pattern, we will search for the words that is by using \w and in conjunction with + for multiple words. Here is the complete URL Pattern for Single Article Clean URL Pages – ^([-\w]+)$

Next Original URL, that is we will use the original url that is single.php?url=article-name. In place of article-name we will retrive the URL patter with $1. And the final Original URL will looks like single.php?url=$1

Next its same flags NC for case-insensitive and L for the end of the rule. Complete rule for Category URLs

6. Making Category URL’s as Clean URLs

Category URL’s are also same as Single Article URL’s, except that here in category URL’s I’m using extra category slug in the category. Starting and ending of URL is same.

Here is category URL structure before and after, that is from sitename.com/index.php?category=category-name to sitename.com/category/category-name

URL pattern will be similar to Single Article Page URL pattern, there will be an added extra category slug with words pattern. Our Complete URL pattern for category URL is ^category/([-\w]+)/(\d+)$

Original URL is same as the previous rule that is single article URL. The pattern will be retrieved as $1. Final original URL for category pages are category.php?url=$1

Next its same flags NC for case-insensitive and L for the end of the rule. Complete rule for Category URLs

7. Making Pagination as Clean URLs for Category Pages

Category URL with pagination looks like this before and after. From sitename.com/index.php?category=category-name&page=2 to sitename.com/category/category-name/2

For making pagination URLs as clean URL I’m going to add extra pattern group to category rewrite rule URL pattern and also to original URL.

This pattern group is completely numbers so I’m using \d with + for multiple numbers. And the Final URL pattern will be (\d+). Complete URL pattern with pagination is ^category/([-\w]+)/(\d+)$

Next is Original URL, here we should retrive second pattern it will be represented as $2 and the final Original URL will look like category.php?url=$1&page=$2

Next its same flags NC for case-insensitive and L for the end of the rule. Complete rule for Category URL with Pagination is

You have to replace the above code with this one.

Creating PHP Clean URLs in Apache with HTACCESS

Are You Excited to Create Clean URLs on your own Projects?

I've good News for you! Created a Video Course with 30 minutes of Content...

8. Fixing Category Clean URls with Pagination Issue

After adding pagination Rewrite Rule, if you access category pagination pages it works fine. If you access category pages, you will get an error.

This is because, whenever you are using multiple URL pattern groups. You should add multiple Rewrite Rules in descending order.

That is you should add Rewrite Rule with multiple URL patterns on the top, then next after that next.

That means, right after the step 6 rule you should add step 5 rule. That will fix the issues.

Complete Code

If you have any problem arranging above pieces of code, you can access the complete code here.


Clean URLs are more important for any web application because search engines love pretty URLs and only rank them in hight positions.

If you are a developer, this is the important skill you should have. I’ve already created video course for you to learn easily. You can join the course.

If you are getting any problem, let me know through the comment form below.

clean url php how to create a simple login page php mysql how to make clean url with apache for blogs login in php mysql example login register php html code simple frome ragistreton page coding
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: