Jump to content

url path question


abrahamgarcia27

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.