-1

I am designing my first blog website and would like to know how to implement SEO friendly url encoding.

At the moment I have this kind of URL:

http://mywebsite.com/article.php?id=2

But what I really want is this kind:

http://www.mywebsite.com/2013/11/09/this-is-my-article-title/

Could someone please point me in the direction a good tutorial for accomplishing this or perhaps explain how I can do it. I have search both here and google haven't yet found anything remotely helpful.

Nick Law
  • 1,580
  • 3
  • 17
  • 27
  • Have a read up on [URL rewriting](https://www.google.co.uk/search?q=url+rewriting). To see how StackOverflow generates their friendly URLs, have a look at [this post](http://stackoverflow.com/a/25486/2765666). – George Brighton Nov 09 '13 at 20:58

1 Answers1

0

You can use something like this (put it in a file called .htaccess in the root of your website):

# Enables rewrite
RewriteEngine On

# Only continue if it's not a directory, file or link
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

# rewrite to file
RewriteRule ^(.*)$ index.php?page=$1

Then for http://yourwebsite.com/homepage you will get a request at index.php?page=homepage so that in PHP you can read out the page with:

if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 'defaultpage';
}

if ($page == 'homepage') { 
    // display homepage
} // etc.

For more information about this (so-called mod-rewrite or URL-rewriting), look up the Apache documentation of this.

Jochem Kuijpers
  • 1,770
  • 3
  • 17
  • 34
  • Can I test the .htaccess file locally or does it have to be on site's server? Because I'm trying to test it on my local machine but now WAMP won't open the site – Nick Law Nov 09 '13 at 21:25
  • @NicholasLaw As long as it's working on an Apache server, it should be fine. But if somehow your local server isn't configured correctly, it might not work. Check if the mod-rewrite module is loaded in Apache. (edit:) Also, the file has to be called `.htaccess`, nothing else! – Jochem Kuijpers Nov 09 '13 at 21:26