blogging
Create SEO Friendly URLs For Your Blog Posts
How does it looks when your new blog post URL have those really weird numbers and symbols in it? It looks more like a URL for robots than humans. And if you have a list with several URL’s, how do you know which one is about what topic? It can get messy. The most important issue though, is SEO. If you want to rank on the first page of the search engine results, then it’s crucial to have a human readable URL or, in another word, an SEO friendly URL. It is one of the most important factors that have a strong impact on search engine ranking. An SEO friendly URL represents the main topic of your blog post, so readers can have the idea by just looking at it. It also helps Search engine to recognize what your post is about. So it’s easy and reasonable to be convinced to get a human readable URL for your blog post. But how do you do that? Fear not! That’s what we are going to show you today!
If you have blog build upon custom PHP code, then there’s two different way you can get a human readable/SEO friendly URL for your post. One is by using the Request_URi method and the second is with the help of .htaccess file. We prefer the second one and that’s what we are going to demonstrate.
Before we begin, let’s recap the scenario. We are going to assume that you have a fluffy cat that loves to sleep on your lap all day. You’ve recently noticed that your cat doesn’t get afraid by cucumber! You got surprised and wrote a blog post about how brave your cat is. Your article have a heading ‘The Bravest Cat Ever!’ After you publish your post, you get a URL for your post which looks something like this:
www.yoursitename.com/index.php?blog_id=5243
Now, the problem with this one is; when you spread this URL here and there on the internet, No one understands what is about until they actually enter your site. Lots of cat lovers like me would visit your site if they knew it was about cats. More importantly, Search engine won’t like your URL either. Chances are your post will never be on the first page for the search keyword: ‘Bravest cat ever’
So, we want to change it to something more understandable and (SEO) friendly. Something like this:
www.yoursitename.com/bravest-cat-ever/
Doesn’t that looks pretty? Sure it does. Let’s go ahead and do that!
Step 1:
What we are going to do is to make a way as such that when we click on the friendly URL, it’ll redirect us to the main content. So, we have to associate our friendly URL with our main content somehow. For that purpose, We are going to create a column in the content table where we’ll store the friendly URL’s.
So, go ahead and modify the table where your blog post is saved. Create a new column in the table and name it ‘seo-url’.
Step 2:
We’ll use a php function to create SEO friendly URL’s for us. Use the below code to create the function.
function coderevolution_seo_url($tt_string) { $tt_string = trim($tt_string); $tt_string = html_entity_decode($tt_string); $tt_string = strip_tags($tt_string); $tt_string = strtolower($tt_string); $tt_string = preg_replace(‘~[^ a-z0-9_.]~’, ‘ ‘, $tt_string); $tt_string = preg_replace(‘ ‘, ‘-‘, $tt_string); $tt_string = preg_replace(‘~-+~’, ‘-‘, $tt_string); $tt_string .= “/”; return $tt_string; } |
This function will take your article’s title and will return as SEO friendly URL. We’ll save this data in the table column that we created in the first step.
Step 3:
Now that we’ve got our SEO friendly URL. It’s time to re-write it to the place of ugly original URL. We need to make some change in our .htaccess file to do that. If you already don’t have the .htaccess file, then create one and insert the following code in it:
RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(([A-Za-z0-9]+[-]+[A-Za-z0-9]+)+[/])$ <span class=”skimlinks-unlinked”>index.php?blog_url=$1</span> [NC,L] # Handle blog requests |
Here at first we are acknowledging apache that we are going to re-write some rules by saying ‘RewriteEngine On’. The second and third line is checking if the URL we are calling is a file or directory; In which case, we won’t re-write anything. And the last line is the main code that’s responsible for URL rewriting.
Step 4:
We’re almost done! Now we can get the SEO friendly URL by using $_GET[‘blog_url’] in our index.php. For example, Your database query for index.php will changed into this:
$url = $_GET[‘blog_url’]; $query = “SELECT articles.article_name,articles.article_content,categories.category_name,<span class=”skimlinks-unlinked”>articles.img,users.u_fname,users.u_lname,DATE_FORMAT(articles.date,’%d</span> %Y’) as dates FROM article INNER JOIN users ON users.user_id = article.user_Id INNER JOIN articles ON articles.article_id = article.article_id INNER JOIN categories ON categories.category_id = articles.category_id WHERE <span class=”skimlinks-unlinked”>articles.url</span> = ‘$url'”; |
Everything except that will remain same. Now all the cat lovers can understand that the article is about cat even before opening the page. Search engine will also like it and you’ll be in favor to get place on the first page.
Some you’ll make the best use of your newly acquired power. If something remains unclear or, if you have any opinion; feel free to leave a comment below. We’ll get back to you ASAP.
You must be logged in to post a comment Login