marcbraulio Posted March 15, 2012 Share Posted March 15, 2012 Hey everyone, I am attempting to find the best method to dynamically create seo friendly urls. This is my plan: Original url: http://www.example.com/index.php?comp=article&view=category&id=1 Make slug from the page's title and add it to url: http://www.example.com/index.php?comp=article&view=category&id=1&/this-title-was-converted-to-a-slug Finally remove "index.php?comp=article&view=category&id=1&/" from the url using .htaccess: http://www.example.com/this-title-was-converted-to-a-slug My questions are: Is there a better strategy/technique for accomplishing this? I have done a lot of research and this seems to be the most popular method. Also since I couldn't do directly put a forward slash right after "&id=1", like so "&id=1/this-title-was-converted-to-a-slug", I went ahead and added a "&" right after the id number, like so "&id=1&/this-title-was-converted-to-a-slug". Is this the correct way of doing this? Any suggestions are appreciated, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/ Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 No, it's not. You're looking for a technique called mod_rewrite, which is a set of regular expressions in your apache config that turn friendly URLs into query strings your pages are actually capable of using. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327542 Share on other sites More sharing options...
marcbraulio Posted March 15, 2012 Author Share Posted March 15, 2012 Thank you for the quick reply No, it's not. This is a response to which question? You're looking for a technique called mod_rewrite, which is a set of regular expressions in your apache config that turn friendly URLs into query strings your pages are actually capable of using. I am aware of that, that's what I meant by "Finally remove 'index.php?comp=article&view=category&id=1&/' from the url using .htaccess", I plan to use mod_rewrite. The slug has to be in the url for mod_rewrite to work with it, am I correct? That's why I am adding it in the second step. I am trying to accomplish this: http://www.example.com/this-title-was-converted-to-a-slug Rather than this: http://www.example.com/article/category/1/ Please let me know if I have this concept wrong. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327660 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 I think you are looking for URI routing. Generally the htaccess will be something like, <IfModule mod_rewrite.c> RewriteEngine on Options +FollowSymLinks -Indexes RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?uri=$1 [PT,L] </IfModule> And thus all requests will tunnel through index.php. From there, $_GET['uri'] will contain the requested segments. So if you went to example.com/article/category/1, $_GET['uri'] would contain "article/category/1". You can explode this string to get an array of each segment. From there, take a look at some URI routing classes. The one for CodeIgniter is really simple but will still do what you need. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327663 Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 The "no it's not" was in response to "Is this the right way to do it?" You can't have JUST the post title in your URL, how would your pages know what to fetch? You usually want your URLs to look like: www.yoursite.com/article/123/How-to-use-mod-rewrite-to-make-seo-friendly-urls Mod-rewrite will turn that link into: www.yoursite.com/article.php?id=123 There is no "removing" of data, mod-rewrite takes a full URL and...rewrites it. You would publish the "pretty" version above, and mod-rewrite would turn it into the "useful" version, stripping off the useless-to-computers bit at the end. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327667 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 You can't have JUST the post title in your URL, how would your pages know what to fetch? You can indeed have just the post title in your URL. Wordpress seems to handle it without problems. The logic seems pretty straight forward. if (physical page exists) { .... } else if (article exists) { .... } EDIT: You would just have to make sure your slugs were unique or you might have conflicts. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327669 Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 EDIT: You would just have to make sure your slugs were unique or you might have conflicts. Right, that's the issue. Forums would crash instantly if we had to uniquely identify "please help" as a thread title. Also, articles aren't good for this either because of the likelihood of adding things like [update] and [Fixed] to the title. Unique IDs all the way. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327714 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 EDIT: You would just have to make sure your slugs were unique or you might have conflicts. Right, that's the issue. Forums would crash instantly if we had to uniquely identify "please help" as a thread title. Also, articles aren't good for this either because of the likelihood of adding things like [update] and [Fixed] to the title. Unique IDs all the way. The slug doesn't have to be identical to the title. For example if your article was titled "Some uber leet article" the slug might be "some-uber-leet-article". If you added [update] to the title, the slug can stay the same. If you ever title something the same, you'd append a number to the slug. So it would become "some-uber-leet-article-2". It really depends what you are doing as to how you want your URL's. Something like a forum, or something where you run a high chance of a duplicate title, you might want to go with something else. Maybe prefix the slug with the post ID or something, so it would become "19284-some-uber-leet-article". Still looks pretty, yet will always be unique. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327733 Share on other sites More sharing options...
ManiacDan Posted March 15, 2012 Share Posted March 15, 2012 That's what devshed does, the ID is before the title. I added a slash instead of a dash for no real reason above. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327741 Share on other sites More sharing options...
marcbraulio Posted March 15, 2012 Author Share Posted March 15, 2012 You can't have JUST the post title in your URL, how would your pages know what to fetch?... ...There is no "removing" of data, mod-rewrite takes a full URL and...rewrites it. You would publish the "pretty" version above, and mod-rewrite would turn it into the "useful" version, stripping off the useless-to-computers bit at the end. Thank you for the explanation, some how I thought it was the other way around. I now see that my way would never work unless every link was redirected. You can indeed have just the post title in your URL. Wordpress seems to handle it without problems. The logic seems pretty straight forward. Would you know exactly how Wordpress deals with multiple components while still keeping the link free of component declarations? If I understood your post correctly, they either: 1. Assume that for any link without a component declaration, like so: (www.example.com/how-to-use-examples) is automatically referring to the article component and therefore the slug is automatically searched for in the article database, if no article is found an error would show. While every other component would require a component declaration, like so: (www.example.com/store/men-blue-jeans) to avoid being searched for in the article database. or 2. Check each component for the slug, like you posted above: if (slug exists in article component) { .... } else if (slug exists in store component) { .... } else if (slug exists in contact component) { .... } But this method seems to be insanely inefficient, wouldn't you agree? As a side note: I think I'll avoid using IDs as I think that the chances of a duplicate titles will be slim. Besides for 99% of the cases, it wouldn't make any sense for me to create two different articles with the same title. Avoiding duplication in general is a good habit for SEO anyway... Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327786 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 In Wordpress everything is stored in the same table "wp_posts", so it can simply search for slugs in that table. If one is found, it displays whatever type of content is associated with that result (whether it be a post, or a page or whatever). Somehow it also figures out if the slug is a category or not. Like I've said before, I hate Wordpress but I really love how they handle permalinks. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327791 Share on other sites More sharing options...
marcbraulio Posted March 15, 2012 Author Share Posted March 15, 2012 In Wordpress everything is stored in the same table "wp_posts", so it can simply search for slugs in that table. If one is found, it displays whatever type of content is associated with that result (whether it be a post, or a page or whatever). Somehow it also figures out if the slug is a category or not. Like I've said before, I hate Wordpress but I really love how they handle permalinks. Interesting, I am going to do a little more research on that. Many thanks for the great insight, this is exactly what I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327799 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 Along with that, I think you might also be interested in URI routing like I mentioned earlier. Usually it is used for MVC platforms but you can adapt it to anything really. Like I said earlier, CodeIgniter's method is really simple. If you download CodeIgniter go to the system/core/Router.php file and check that out. In particular, the _parse_routes() method. This is where the magic happens, and it's really the only code you need to replicate the process. With CodeIgniter, your route would look like: $route['(:any)'] = 'article/view/$1'; This would basically match anything after your domain and send it to the controller "article" and method "view". Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327811 Share on other sites More sharing options...
marcbraulio Posted March 15, 2012 Author Share Posted March 15, 2012 Along with that, I think you might also be interested in URI routing like I mentioned earlier. Usually it is used for MVC platforms but you can adapt it to anything really. Like I said earlier, CodeIgniter's method is really simple. If you download CodeIgniter go to the system/core/Router.php file and check that out. In particular, the _parse_routes() method. This is where the magic happens, and it's really the only code you need to replicate the process. With CodeIgniter, your route would look like: $route['(:any)'] = 'article/view/$1'; This would basically match anything after your domain and send it to the controller "article" and method "view". Awesome, I'll definitely check it out. Coincidentally the CMS I am building for my own use is based on my experienced with Joomla and Codeigniter. So far I been using Joomla's way of getting the controller and method, probably should have went towards Codeigniter for that one. The reason I got Joomla's concept involved was because I very much like their folder structure, frontend/backend separation, and system of organizing components and modules while utilizing the MVC architecture, which is something that I really believe Codeigniter is weak in, even with the "HMVC" plugin. I am putting it up on GitHub soon, I'll send you the link if you would like to take a look at it. Quote Link to comment https://forums.phpfreaks.com/topic/258963-search-engine-friendly-urls/#findComment-1327840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.