-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.

1

1 Answer 1

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.

2
  • 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 9, 2013 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! Nov 9, 2013 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.