Create SEO Friendly URLs With Htaccess Mod Rewrite
Make SEO friendly URL yourself
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.
1. Creating .htaccess file
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 |
2. Create your own rewrite rule
Example 1For 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 |
• 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
Example 2
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 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.
Example 3
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 backslash here “escapes” the dot, so that the dot means a real dot instead of “anything”.
3. Extra Stuff
Custom 404 error page
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 |
Disable directory browsing
For security purpose, its best to disable directory browsing so that people won’t know what files you have. Use this :Options All -Indexes |
Protect .htaccess files
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>
source: http://zenverse.net/seo-friendly-urls-with-htaccess/
This is a very nice article on htaccess seo url i like your article.
ReplyDeletenice article on Htaccess rewrite thanks
ReplyDelete