Jump to content

Recommended Posts

I have a question on how to structure my url for a blog. Before i used the ID of the blog_id, but i feel i should add the title of the post (Like they do on this forum). Then i added the the title to the blog to query the blog post, but i came across the problem that the title was similar. I am just trying to get some knowledge from my fellow friend on how to structure a url path for SEO. I use .htaccess for rewriting my urls this is the structure i use.

 

Also some people added characters which broke my query like (;,"'@^, etc...)

 

index.php?action=blogpost&title=$title /blog/post/$blog_title

index.php?action=blogpost&blog_id=$blog_id /blog/post/$blog_id

Edited by abrahamgarcia27
Link to comment
https://forums.phpfreaks.com/topic/284618-url-path-question/
Share on other sites

Use both the ID and title.

 

Otherwise you have to make sure the title is unique for every post (which, you know, it kinda should be) but you can do that easily enough by assigning each post a specific "title" just for the URL.

 

As for people breaking your queries, you're doing something very wrong. Post your code.

Link to comment
https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461700
Share on other sites

Ok this is what i am using for .htaccess

RewriteRule    ^post/([A-Za-z0-9-]+)/?$      index.php?action=blogpost&title=$1    [NC,L]

This is how i send the title to query 

 $urlTitle = strtolower(str_replace(' ', '-', $blogs['title']));

<div class="post-meta-readmore"><a href="<?php echo SITE_PATH; ?>post/<?php echo $urlTitle; ?>">Read more</a> </div>

And this is how i query the post

function getBlog($title){
	
	$string = str_replace('-', ' ', $title);
	$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
	$sql = "SELECT * FROM `".TABLE_BLOG."` LEFT JOIN `".TABLE_BLOG_CAT."` ON `".TABLE_BLOG."`.category_id = `".TABLE_BLOG_CAT."`.category_id WHERE title = '$string' LIMIT 1";
	$record = $db->query_first($sql);
	return $record;
}
Link to comment
https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461870
Share on other sites

it is most probably the colon that is breaking the url. The colon has a special meaning in urls as it usually defines the http port.

 

Urls should only contain letters, numbers, dashes, underscores and periods. Any other character should be urlencoded. However this will make your urls messy if start encoding non url characters.

 

What you are better of doing is adding another field to your database that stores the url for the page, and the query the database that matches this field instead of the title field.

 

So basically you need to make the entered title url safe. The following will make the title url safe

$page_url = str_replace(' ', '-', strtolower($page_title));
$page_url = preg_replace('~[^a-z0-9_\-]+~i', '', $page_url);

So if you enter this as the title "The New Era of Music: Indie, Metal, Alternative" it will generate "this-the-new-era-of-music-indie-metal-alternative" as the page url.

Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461874
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.