Search code examples
.htaccessurl-rewritingsef

Create search engine friendly urls for our blog


We run a blog, and really need to tidy up the URLs using htaccess, but I am really stumped.

Example:

Working on a site, and I need to generate search engine friendly URLs

So I have the url currently as:

http://mywebsite.com/blog/read.php?art_id=11

Title of this page is:

Why do Australians pay so much for Cars ?

I need to change it to its corresponding SEF url. like so:

http://mywebsite.com/blog/Why-do-Australians-pay-so-much-for-Cars-?

The question mark is part of the title, and we could remove these if its a issue. Any suggestions please?

Also would prefer to drop the read.php portion. Need to create a rule that works across our entire blog.

They all follow the same pattern, only the art_id number changes.


Solution

  • (Assuming that you're using apache as a webserver)

    Take a look at this answer for a very similar question: https://stackoverflow.com/a/8030760/851273

    The problem here is that .htaccess and mod_rewrite doesn't know how to map page names to art_id's so there's 2 ways you can try to do this.

    1. You can add some functionality to your read.php so that it can do a similar lookup but instead of art_id, it uses art_title or something. Essentially you'll have to do the backend lookup of a database (or wherever your articles are stored) and use the title as a key instead of the ID. This is a little messy since it's possible to have weird characters in titles such as non-ascii or reserved characters (like ? for instance), so you'll need to create a title encoder and decoder when pulling titles out of the database or when using titles to lookup an article in your database.
    2. If you have access to the server config or vhost config, you may be able to setup a RewriteMap using an outside program (the prg type) and create a php script that does the title-to-ID lookup for you. Then you can create rewrite rules in your .htaccess that does something along the lines of:

      RewriteRule ^blog/(.*)$ /blog/read.php?art_id=${title-to-id:$1} [L]
      

      Where you are extracting the article title from your pretty URL, and feeding it through a rewrite map called title-to-id to get the art_id. Again you'll need to setup a title encoder/decoder so your titles will have the non-ascci and reserved characters dealt with.

    Another thing that you can do is to stick an article ID in your pretty URLs so they look like this: http://mywebsite.com/blog/11-Why-do-Australians-pay-so-much-for-Cars. This is still pretty easy to see what the link is about, it's SEO friendly, and it bypasses the need to do title-to-ID lookups. The Rewrite Rules would also equally be simpler:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # add whatever other special conditions you need here
    
    RewriteRule ^blog/([0-9]+)-(.*)$ /blog/read.php?art_id=$1 [L]
    

    And that's it. Of course, you'd have to now generate all of your blog URL's to be of the form: http://(host)/blog/(art_id)-(art_title), and you'd also have to remove special characters from the title, but you don't have to worry about writing additional code to translate titles back to IDs.