An example of SEO friendly URL can be seen in this page. Compare it with urls like http://mysite.com/?t=34 , you will find it easier to remember and the it also clearly tells you what this page is about. Furthermore, the words in the URL might match the search keywords, bringing more traffic from search engines.
I don’t have to worry about SEO friendly URL in WordPress since its already built-in. However, I need it for my premium wordpress themes website, which is not based on WordPress. So I started to look into this matter.
Using htaccess and mod rewrite, you can make SEO friendly yourself in 3 easy steps.
Open NotePad (yes, the windows notepad) > File > Save As > Change the “Save as Type” to “All Files” > enter “.htaccess” as the name and press Save. You’ve created a htacces file.
Now paste this codes below into it.
Options +FollowSymLinks RewriteEngine On
The first line Options +FollowSymLinks is required for some server configurations.
For example, if you want to change links like http://mysite.com/index.php?topic=rules to http://mysite.com/topic/rules/, here’s the rewrite rule.
Options +FollowSymLinks RewriteEngine On RewriteRule ^topic/([a-zA-Z0-9]+)/$ index.php?topic=$1
• Like regular expressions, the [a-zA-Z0-9] matches lower and uppercase of alphabets and numbers.
• The asterisk inside the brackets + is a quantifier that match 1 occurence to infinite occurences.
• Combining them, ([a-zA-Z0-9]+) matches alphanumerics of at least 1 character.
• The caret ^ means “start with”, meaning the URL starts with the word “topic”.
• The dollar sign $ means “end”, meaning the URL ends with a slash.
• The $1 is backreference, it carries the content of the first group of brackets.
In other words, when user enters http://mysite.com/topic/faqs/ , the page being called and run would be http://mysite.com/index.php?topic=faqs
If you want to change URLs like http://mysite.com/index.php?product=productname&price=30 to http://mysite.com/products/productname/30/. Basically its similar to above.
Options +FollowSymLinks RewriteEngine On RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?product=$1&price=$2
• The [0-9] in matches numbers only.
• The plus sign is a quantifier that match 1 or more occurences.
• Combining them, ([0-9]+) means 1 or more numbers.
• Similarly, $1 will be the first brackets : product name and $2 would be the second brackets : price.
If you want to change URLs like http://mysite.com/article.php?id=45 to http://mysite.com/article-45.html, here’s how:
Options +FollowSymLinks RewriteEngine On RewriteRule ^article-([0-9]+)\.html$ article.php?id=$1
• The new thing here is the \. (backslash followed by a dot).
• The backslash here “escapes” the dot, so that the dot means a real dot instead of “anything”.
Put this in your htaccess if you would like to have a custom 404 error page instead of the default one.
ErrorDocument 404 /404.php
Change the 404.php to your page.
For security purpose, its best to disable directory browsing so that people won’t know what files you have. Use this :
Options All -Indexes
This should disallow other to access your .htaccess file, just like disallowing others to access your wordpress’s wp-config.php
<files .htaccess> order allow,deny deny from all </files>