Friendly urls SEO url for search engines using htaccess examples

What is a SEO URL or a search engine friendly URL ?

Before we learn how to create SEO URL or a search engine friendly URL we need to understand what it is and what is it’s purpose. A SEO or search engine friendly URL is the one that gives good information on what a particular URL contains. For example: Lets say look at following article – SEO tutorial for beginners SEO tips SEO tools. This article can be accessed via two different links:

  1. (SEO or Search engine friendly URL)
  2. (Non SEO or non search engine friendly URL)

The first URL contains information on what the article is about. This allows search engines to identify the correct content of the page. This SEO technique in turn makes your article rank higher in search engine rankings.

The second URL on the other hand gives no idea on what the page is about. Note that second link is redirected to point to firstĀ  URL via 301 redirects. This allows search engines to consider first URL as the correct URL to index and show in the results. You can learn how to create 301 redirects at htaccess 301 redirect examples.

How to create SEO or search engine friendly URLs with htaccess examples?

Apache web server allows rewriting URLs to be more search engine friendly. For this to work you would need Apache to support rewriting module or mod_rewrite. Lets look at how to do this with the help of real examples.

Lets assume that URL A (shown below) is currently the supported URL for your website. However we now want to support a new search engine friendly URL B for the same content. In this example we look at converting URL B to A such that when a user requests the resource using URL B it gets converted to URL A internally and thus correct data is fetched.

A) (Non search engine friendly URL)

B) (Search engine friendly URL)

htaccess example 1:

RewriteEngine On
RewriteRule ^example/(.*)$ show_example.php?name=$1 [L,NC]

The first line is quite self explanatory. It asks Apache to turn on the Rewrite engine so that it can process any rewriting of the URLs.

In the second line has 4 parts separated by spaces.

The first part is just the keyword RewriteRule to let Apache know that what follows is the actual rule.

The second part defines the URL that needs to be rewritten internally. So this should match the SEO friendly URL B above. The part (.*) specifies Apache to capture the portion of URL from “example/” till end of URL ($) and place it into variable $1.

The third part matches the format of non SEO friendly URL A. This is what the URL that matches in second part above should be converted to internally.

Finally in the fourth part [L,NC] L specifies that no more rules should be processed and this should be treated at Last rule and NC specifies that there should be no case consideration (Uppers vs lower case).

The issue here is that there is no redirect for URL A and both URLs serve same content. In order to handle that use canonical meta tag. Canonical tag would make sure that search engines understand that both the pages are serving same data and the actual visible URL should be one that is specified in the canonical tag.

We look at other htaccess examples where in we can transfer all control to a PHP file which can then manipulate or redirect the URLs.

htaccess example 2:

    ForceType application/x-httpd-php

The code above forces all matching files to be served as php files. Therefore a URL such as will try to execute a file test as a php file. Now you can take action in test file based on the $_SERVER[‘PATH_INFO’] variable as follows:

// outputs '/oldurl.html'

Another way of achieving this is illustrated with example shown below
htaccess example 3:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

This does pretty much the same thing as above. It really means that if a requested resource is not a file or a directory then call index.php. You can modify this to meet a different criteria and then call a different file.

SEO friendly url

How to create SEO or search engine friendly URLs for WordPress ?

WordPress makes it quite easy to create friendly URLs. In case of WordPress, the term used for friendly URLs is permalinks. All you need to do is manage permalinks in your settings. Details on how to manage permalinks can be referred from the WordPress article here. WordPress uses same mechanism as shown in example 3 above.

Related Posts