SEO friendly URL with .htaccess
Wednesday, May 08, 2013
The words in the URL might match the search keywords, bringing more traffic from search engines. The seo friendly url is like to same as that http://mysite.com/index.php?product=productname&price=30 to http://mysite.com/products/productname/30/. I have no worry about the seo friendly url because I used to like htaccess and mod rewrite. It is so easy to rewrite a url to seo friendly.
Creating .htaccess file: Just open a notepad (windows notepad) and save the file as ".htaccess" without any extension. Because .htaccess is the extension. It’s not htaccess.html or index.htaccess; it’s simply .htaccess.
Server configurations: Write in file the two lines.
- Options +FollowSymLinks
- RewriteEngine On
Create your own rewrite rule:
Example 1: 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.
Example 1: 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 occurrence to infinite concurrences.
• Combining them, ([a-zA-Z0-9]+) matches alphanumeric 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 back reference, 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/. here’s the rewrite rule.
- 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.
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 the rewrite rule.
- 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”.
Example 4. Redirect non-www to www:
- RewriteEngine On
- Options +FollowSymLinks
- RewriteCond %{HTTP_HOST} ^mysite.com [NC]
- RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Example 5. Redirect www to non-www:
- RewriteEngine On
- Options +FollowSymLinks
- RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
- RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
Example 6. Redirect an old page to a new page.
- Options +FollowSymLinks
- RewriteEngine on
- Redirect 301 /oldpage.html http://www.mysite.com/newpage.html
Example 7. Redirect several pages to one as like example 6
- Options +FollowSymLinks
- RewriteEngine on
- Redirect 301 /seattle-companies.html http://www.mysite.com/seattle-company.html
- Redirect 301 /seattle-locations.html http://www.mysite.com/seattle-company.html
Example 8. Hide extention of the page as http://www.mysite.com/news.php to http://mysite.com/news.
- Options +FollowSymLinks
- RewriteEngine on
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME}\.php -f
- RewriteRule ^(.*)$ $1.php
Example 9. 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.
- Options +FollowSymLinks
- RewriteEngine on
- ErrorDocument 404 /404.php
Example 10. Disable directory browsing for security purpose, its best to disable directory browsing so that people won’t know what files you have.
- Options +FollowSymLinks
- RewriteEngine on
- Options All -Indexes
Example 11. Redirect 301 permanent, Search engine bot that visits the
old file will be redirected to the new site. Site is redirect to the
http://www.newsite.com from your site as te http://www.mysite.com
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
Some others Command Flag Block:
[R] Redirect you can add an =301 or =302 to change the type.
[F] Forces the url to be forbidden. 403 header
[G] Forces the url to be gone 401 header
[L] Last rule. (You should use this on all your rules that don't link together)
[N] Next round. Rerun the rules again from the start
[C] Chains a rewrite rule together with the next rule.
[T] use T=MIME-type to force the file to be a mime type
[NS] Use if no sub request is requested
[NC] Makes the rule case INsensitive
[QSA] Query String Append use to add to an existing query string
[NE] Turns of normal escapes that are default in the rewriterule
[PT] Pass through to the handler (together with mod alias)
[S] Skip the next rule S=3 skips the next 3 rules
[E] E=var sets an enviromental variable that can be called by other rules
Protect .htaccess files: This should disallow other to access your .htaccess file, just like disallowing others to access your wordpress’s wp-config.php
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
Some others Command Flag Block:
[R] Redirect you can add an =301 or =302 to change the type.
[F] Forces the url to be forbidden. 403 header
[G] Forces the url to be gone 401 header
[L] Last rule. (You should use this on all your rules that don't link together)
[N] Next round. Rerun the rules again from the start
[C] Chains a rewrite rule together with the next rule.
[T] use T=MIME-type to force the file to be a mime type
[NS] Use if no sub request is requested
[NC] Makes the rule case INsensitive
[QSA] Query String Append use to add to an existing query string
[NE] Turns of normal escapes that are default in the rewriterule
[PT] Pass through to the handler (together with mod alias)
[S] Skip the next rule S=3 skips the next 3 rules
[E] E=var sets an enviromental variable that can be called by other rules
Protect .htaccess files: This should disallow other to access your .htaccess file, just like disallowing others to access your wordpress’s wp-config.php
- Options +FollowSymLinks
- RewriteEngine on
- <files .htaccess>
- order allow,deny
- deny from all
- </files>
Labels: Others