Simple Steps for Creating Friendly URL’s with PHP & HTACCESS
Here in this blog, we will tell you how you can create friendly URL’s with PHP & HTACCESS.
One of the most helpful functionalities of the htaccess file is the capacity to alter URLs. You can use it to redirect one URL to another or use it to create a broad sweeping change to all URLs on your website.
In this post, you are going to learn about generating Clean URLs in PHP with apache htaccess. Here I’ll be using a straightforward content management system application developed in PHP & MySQL. From this article, you can read about the content management system. You can unquestionably create clean URLs in your projects after completing this article, it’s simple to follow but you need to understand some basics about regular expressions.
About the PHP CMS Project Demo Clean URL
You can readily learn to create Clean URL’s in PHP with apache htaccess. In the content management system, I have developed four kinds of pages, they are index.php that this home page used to show all the articles and I also use index.php file to show category-based articles with a URL query string that looks like sitename.com/index.php?category=category-name, I’ll be altering this URL to clean URLs as sitename.com/category/category-name.
And the next page is a single article page with a single.php and URL structure before clean URL is sitename.com/single.php?url=article-name after making it as clean URL is sitename.com/article-name. And you’ll also learn to eliminate the .php extension from URLs.
In the category pages, we have pagination, I’ll demonstrate to you how we can convert them to clean URLs, pagination number after the category name. Original category page pagination looks like this sitename.com/index.php?category=category-name&page=2, We’re going to create them as sitename.com/category/category-name/2
First of all, check whether or not mod_rewrite apache module is activated. You can test it by generating a new file with the phpinfo function. Open this php file in the browser after searching for mod_rewrite. You should see this as enabled.
If the mod_rewrite module is enabled, you can proceed with the next steps.
Steps to Use Mod_Rewrite:
Create a file and save it as a.htaccess, then switch on the rewrite engine in the.htaccess file by adding RewriteEngine on Then after that Create Rules to match URL requests for clean URL’s.
Example of htaccess Rule
Here is the instance of htaccess rule.
RewriteRule URL Pattern Original URL [flags]
Every rule starts with Rewrite Rule followed by URL Pattern, which is the frequent expression. After that its Original URL, then flags.
Key Points to Remember while working with htaccess and mod_rewrite
Every new rule should be in a different row. Test them after creating rewrite rules. Also, check inner & links files linking (relative to site root). And apache takes care of loading the right files internally.
Making Clean URL’s for Single Article Page
I’m going to transform URL from sitename.com/single.php in a single article page? url = article-name to sitename.com/article-name. To do this, we need to change the pattern of the URL and the Orignal URL, and everything is the same. That’s rules that start with RewriteRule and the flags.
In URL Pattern, we will search for various phrases by using \w and in conjunction with +. Here is the full URL Pattern for Single Article Clean URL Pages – ^([-\w]+)$
Next Original URL, will we use the original single.php url? url= article-name. We will retrieve the URL patter with $1 instead of the article-name. And the final Original URL will look like single.php? url=$1
Next its same NC flags for case-insensitive and L for the end of the rule. The complete rule for Category URLs.
RewriteRule ^([-\w]+)$ single.php?url=$1 [NC,L]
Making Category URL’s as Clean URLs
Category URLs are also the same as Single-Article URLs, except that using additional category slug in the category here in the URL category. Starting and ending the URL is the same.
Here is the structure of the category URL before and after, that is from sitename.com/index.php?category=category-name to sitename.com/category/category-name
URL pattern will be comparable to Single Article Page URL pattern, an additional category slug with word pattern will be introduced. Our complete URL model for category URLs is ^category/([-\w]+)/(\d+)$
The original URL is the same as the past rule, which is a single article URL. The pattern is obtained as $1. The final original category pages URL is category.php? Url=$1
Next, it’s same flags NC for case-insensitive and L for the end of the rule. The complete rule for URLs in categories.
RewriteRule ^category/([-\w]+)/$ category.php?url=$1 [NC,L]
Making Pagination as Clean URLs for Category Pages
This looks like the category URL with pagination before and after.
Add additional pattern group to the category rewrite rule URL pattern and also to the original URL to make pagination URLs as a clean URL.
This pattern group is total numbers, so use \d with + for various numbers. And the final pattern of the URL will be (\d+). The complete paginated URL pattern is ^category/([-\w]+)/(\d+)$
Next is the original URL, here we should retrieve the second pattern that will be represented as $2 and the final original URL will look like category.php? url=$1&page=$2
Next its same NC flags for case-insensitive and L for the end of the rule. The complete rule for the Pagination URL category is:
RewriteRule ^category/([-\w]+)/(\d+)$ category.php?url=$1&page=$2 [NC,L]
You have to substitute the code above with this one.
Fixing Category Clean URIs with Pagination Issue
After adding Rewrite Rule pagination, it functions great if you access category pagination pages. You will get an error if you access category pages.
This is because, whenever you use multiple pattern groups of URLs. In descending order, you should add multiple rewrite rules.
That is, you must add Rewrite Rule to the top with multiple URL patterns, then next after the next one. This will fix the issues.
We explained in a very simplified way how to create friendly URLs with HTACCESS and PHP. Clean URLs are doubtlessly crucial for any web application. As search engines prefer tidy and clear URLs and rank them in higher positions. So being a developer, you ought to have this skill. If you have any problems, please let us know through the comment below.
Latest posts by Arpatech (see all)
- Successful YouTube Campaigns that Will Help Your Startups in 2019 - August 23, 2019
- Android 10 is the official name for Android Q - August 23, 2019
- Top Social Media Marketing Tips to Revamp Your Marketing Strategy in 2019 - August 23, 2019