Dec 26, 2024

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's engagement. In this document, we are going to see how we can use SEO-friendly URLs in the PHP environment with the help of a 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 local host and make some test requests. . .

<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. 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 is 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 visit request yourdomain.com/about-us, You know the slug is about-us. Then you can include that page with the PHP include() function. But for added security, I suggest you keep an array of all existing page slugs like the following.

$pages = ('about-us', 'contact-us', 'posts');
So before you try to include a page, check whether the requested slug is in the array or not using the 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.

ABOUT HACKSLAND

Well explained and interesting cyber security articles and tutorials on the topics such as System exploitation, Web application hacking, exploit development, malwara analysis, Cryptography etc. Let's explorer the awesome world of computer

CATEGORIES
SOCIAL
RANDOM ARTICLES