shanejones Posted May 24, 2011 Share Posted May 24, 2011 Rather than me have all my pages as domain.com/index.php?id=1 etc I would like to pull the id's title from the database to create the URL so that they will look like domain.com/this-is-the-page-name/ Wordpress does it exactly how I want i just need a nudge in the right direction. Anyone know how to do it or now of a guide to help me. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/ Share on other sites More sharing options...
gizmola Posted May 24, 2011 Share Posted May 24, 2011 Doing that is generally referred to as making a slug. You should write a function that sluggifies your title (replaces punctuation and spaces) and makes a unique string that you will need to save in your title table. It would be ideal if you had a script other than index.php so you could do a rewrite based on domain.com/story/slug.... but regardless the rest of the magic is typically done using mod_rewrite rules. The main confusion point for people seems to often be the links they need to use. When you supply links in your blog you need to output the rewritten form of the link. Nothing will automatically rewrite your output, so in other words, once your rewriting is working all your links should be either /story/slug or /slug depending on how your rewrite rules are working. Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219767 Share on other sites More sharing options...
shanejones Posted May 25, 2011 Author Share Posted May 25, 2011 I have the slugs in my database all ready to roll its just working out the correct .htaccess code to get it to access a query. Can't seem to find any real world examples. Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219905 Share on other sites More sharing options...
gizmola Posted May 25, 2011 Share Posted May 25, 2011 Do you have a modification that takes the slug and looks up the story rather than the id? For example: index.php?slug=this_is_my_story You are going to need to to work prior to the rewrite. What I'd recommend is that you artifically append .html to the end of your slug urls. So the links that you would have to your stories would be: Latest Story Of course for this to work, you can't have any other .html url's for your site, or if you do you will need a specific rewrite rule to handle them so that the rule that will rewrite these slugged urls won't mistakenly rewrite those. With that said something like this in your site .htaccess is typical RewriteEngine On RewriteRule ^([0-9a-z_\-]*)\.html index.php?slug=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219909 Share on other sites More sharing options...
shanejones Posted May 25, 2011 Author Share Posted May 25, 2011 The way I was going to do it was linking to the page with <a href="products/this-is-the-product-slug">The Page</a> so nowhere on the site can you see "products/index.php?id=1" Need to hack wordpress I think to see how they do it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219929 Share on other sites More sharing options...
gizmola Posted May 25, 2011 Share Posted May 25, 2011 They do it exactly the way I showed you. If you don't want to use the .html extenstion (which is a really good idea in this case for various reasons, not to mention highly SEO friendly) then you can just omit that from the regular expression in the RewriteRule I provided. RewriteEngine On RewriteRule ^([0-9a-z_\-]*) index.php?slug=$1 [L] I did omit the carat from the previous example .. that should have been there, but I have gone back and editted it. Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219943 Share on other sites More sharing options...
floridaflatlander Posted May 25, 2011 Share Posted May 25, 2011 I got this to work easily, http://www.desiquintans.com/cleanurls Of course I only wanted to go one level deep in my member file so it would be member/joes-fish-mart. Every thing else is retrieved with queries using get. Which is how I like it. Quote Link to comment https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/#findComment-1219967 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.