Jun 20, 2020

SEO friendly url with PHP

When it comes to search engine optimization, URL structure plays an important role. If the URL structure of your website is easy to understand and it is readable then you have a good chance of getting more people engagement.. In this document, we are going to see how we can use SEO friendly URLs in the PHP environment with the help of htaccess file.

What is an SEO friendly URL?

Here you can see a classical HTTP parameter based URL. 


THE above URL is not user friendly. Also, it is not easily readable by users. Actually, when we develop a website, we don't expect users to remember URLs. But it is nice if users can get an idea when they read the URL. What about the following URL.


Any user who visits this page may understand that this blog post is about GDB.  So guys without wasting any time let's get into the coding part.

How to use SEO friendly URLs with PHP?

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?slug=$1 [L]

Here is the code of router.php. It simply fetches the GET parameter called slug and prints it. Actually, before we print it we encode all special characters to prevent XSS.

echo htmlspecialchars($_GET['slug']);

Now I put this on my localhost and make some test requests.





$slug = $_GET['slug'];
$slug = trim($slug);
$slug =  htmlspecialchars($slug);

$parameters = explode('/', $slug);



You may use the parameter array to do the relevant tasks with the requested URL.


So now our URL part is done. If you want to get a Blog post with a requested URL, The best option is to save URL slugs in the database when you make a new post.

Let's say you write a post about GDB. Then you want to make a unique URL slug something as debugging-binaries-with-gdb. Then save it with the relevant post. So every post has a unique slug. When you receive a request to view a post first you should extract parameters and take the slug. Then check your database to see if there a post relevant to the requested slug. If it is, then get that relevant post and show it. If there is no post, show a 404 error.

Also, you can use this SEO friendly URL structure for your pages. You can name your pages with slugs. For example, you may rename the about us page with about-us.php. So when a visited request yourdomain.com/about-us, You know the slug is about-us. Then you can include that page with PHP include() function. But for added security, I suggest you keep an array of all exiting page slugs like the following.

$pages = ('about-us', 'contact-us', 'posts');

So before you try to include a page, check whether requested slug is in the array or not using in_array() function. If it is not in the array just show a 404 page.

So guys that's all for the tutorial. Thank you for reading.

Aug 12
Format strings | Python programming

Format strings are used to format the printing output. We can print a string with variables in it.....

Aug 12
PHP introducing tutorial

Hello guys, in this tutorial I am going to give you a quick idea about php language. We use php for....

May 16
Stack architecture theory tutorial

The stack is an important concept in computer science. If you are planning to learn reverse....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.