Creating SEO friendly URLS using htaccess in PHP -

Learn how to get rid of GET Variables from your page URL

The Idea 💡

First of all, this blog specifically aims for converting GET variables based URL into a SEO friendly URL for a PHP based project.

So if that's it, then let me tell you the core idea : "Store a new column in the database to uniquely identify your Blogs or User or Service etc." And yes, that's it. I know it will take quite a lot of changes if you have already made most of the project but let me assure you this method is going to be easier than converting your project into Controllers, Views and Routers which is the only other way in PHP and followed in MVC Frameworks, Laravel etc.

Now if you are proceeding then I am assuming you have made your decision to bail on that Class structure and is going with this. So to brief you, this is what we are going to do :

1️⃣ We will use .htaccess file to redirect any URL request index.php
2️⃣ Then we will use index.php to make our Router and according to URL request we will send to the appropriate Page or View
3️⃣ Then we will make a function to convert a string to a SLUG, to uniquely identify the data.

🔥 Let's do it !

Using .htaccess file to redirect all URL request to index.php

Using this code, we can redirect all URL requests to index.php(or any file for that matter).

Let's drill in the code :

RewriteEngine On
Starting the rewrite engine

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This checks if the file with the specified name in the browser doesn’t exist, or the directory in the browser doesn’t exist then proceed to the rewrite rule below.

RewriteRule ^(.*)/?$ index.php?url=$1 [L,QSA]
In the RewriteRule the symbol " ^ " implies start of the rule and the symbol " $ " implies end of the rule. " (.*)/? " defines the characters to be allowed in the URL and finally the following the domain or current folder name (Folder in which .htaccess file is residing). In this case we have allowed all small and capital english alphabets, numbers 0 to 9 and " ?,/ " special characters. You can restrict the allowed chars using " [a-z0-9] " inside brackets etcetera. Finally we have took all the characters and redirected it to index.php(you can use any filename if you have anything else) and stored in the $_GET["url"].

Identifying Pages in index.php

Let's say ( and as I prefer ) that you have your views in a views/ directory. And inside the views directory we have all the different views present inside your project.

Why Include condition for Error Page ?
This allows us to include a custom page for the case your project don't have a page ready or maybe just eh user is trying to get unauthorized access by changing the link. Using .htaccess file we can also include a custom Page Not Found Page.

Converting a string to a slug, i.e. a uniquely identifying string

This function takes a string input and returns a slug, a url encoded string which will eventually be your SEO Friendly link. So just convert your blog title to a slug and you are good to go.!

Store the slug into your table and check if the entered URL exists for a blog and if it does include your particular blog view.

Note : If you have more than one table having data for various pages then you should add a text to all slugs so that you can identify which table you have refer to.
Example : hello-world-blog, seo-friendly-blog, etc.

And while checking URLs store last 5 characters of the URL and check it is equal to "-blog" and if it is then fetch blog details.

Why SEO friendly URLs ?

Along with your content, title and meta tags, and link anchor text, search engines use your page’s URL to understand what your content is about. Potential visitors also read the URL to get an idea of what they will experience if they visit your page.

In fact, your URL structure is so vital that Google provides instruction on it in its SEO starter guide. But, if you want to boost your visibility on the web, you have to understand SEO ranking factors.

Specifically, Google takes a variety of factors into consideration that involve your URL. Some of these include your:

🔹 URL length
🔹 URL path
🔹 URL keyword inclusion
🔹 URL string

In all your URL string should be unique for each page and should also relate to the contents of the page. That's why it is preferred to use Blog titles to covert it into slug.

Comments :

Post a Comment :

Contact Us

Contact Info

Let's Connect With Us

Matrix Infotech Solution is your digital destination if you’re looking for customised IT solutions.