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. 

https://hacksland.net/view-post.php?post-id=457

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.

https://hacksland.net/debugging-binaries-with-gdb

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.

<?php
echo htmlspecialchars($_GET['slug']);

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

seo-freindly-slug-1

.

seo-freindly-slug-2

.

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

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

print_r($parameters);

?>

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

seo-friendly-slug-2

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.

Jun 06
Best programming languages for hackers

Do you want to learn hacking? That's great, it is an awesome topic to learn. Some of you may....

Oct 16
Protostar Stack3 Tutorial

Hello there in this article we are going to see how we can exploit and win protostar stack 3 level.....

Jun 13
Build A Simple Web shell

A web shell is a piece of code written to get control over a web server. It is helpful for....

Replying to 's comment Cancel reply