Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/237372-friendly-urls-from-a-database/
Share on other sites

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.

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]

 

 

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.

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.

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.