Create Seo-Friendly URLs with htaccess – URL Rewriting

How to create SEO-Friendly URLs with htaccess . How to Rewrite dynamic URLs to SEO-Friendly URLs using htaccess . In this URL rewriting tutorial, You’ll learn htaccess directives through which you can rewrite existing dynamic URLs into more meaningful SEO-Friendly URLs.

Now you are thinking why we need to create SEO-friendly URLs, what’s their need. First, let’s  understand why  SEO-friendly URLs are important.

Why Seo-Friendly URL ?

Let’s first compare two URLs.

Let’s think for a moment which URL is better and most relevant. Definitely, the first one. So here are the few points why we consider SEO-friendly URLs.

1. Seo-friendly URL’s is easier to remember.

2. It improves your website/blog search ranking and friendliness.

3.  A website with short and relevant URLs are most liked by their end user.

What is URL Rewriting

Most CMS generate dynamic pages using query string parameters. Something like this

This kind of URLs is irrelevant for a search engine as well as for end users. It’s much better if a URL is like this:

Clearly, this one is much cleaner and shorter URL. It’s much easier to remember, and vastly easier to read out. There is no doubt SEO-Friendly URL is better for their end user and search engines.  But remember this URL can’t understand by a server without some work.

URL rewriting is the mechanism to redirect SEO-Friendly URL internally to the original one (Dynamic URL) so that server can understand, which resource user is looking.

Advantages of Url Rewriting

1. Improves the SEO of a website.

2. Cleaner and shorter URL.

3. Hides inner workings of website address (such as query strings).

What is .htaccess

htaccess files are used to provide configuration changes. This file is used by Apache WebServer to read the configuration and directives. Then it applies these directives to directory or subdirectory level (Depends on where you placed your .htaccess file).

Read this htaccess tutorial to learn more about .htaccess file and their usage.

.htaccess files are enabled by default. If it’s not enabled then check the AllowOverride Directive in the httpd.conf file. The RewriteRule is worked only when your server  has mod_rewrite module enable and installed.

What is mod_rewrite in Apache

mod_rewrite is an Apache URL rewriting module. It allows server-side manipulation of requested URLs. The main usage of mod_rewrite is to transform long, confusing URLs into seo-friendly clean URLs.

Long confusing URL:

Clean URL:

How it works

You typed some URL (let’s say – in a browser . This URL is checked with series of rules written in a htaccess file. If the entry is found it will internally redirect to original URL/pattern written in htaccess.

So end user and search engine are able to see the resource using this URL ( But server internally redirects to original url to fetch the resource.

Create SEO-Friendly URLs with htaccess

We have learned so much about the importance of seo-friendly URLs. To start with URL rewriting basics, I assume you have a basic knowledge of Regex. If you are not familiar with regex, then check my previous article on regular expression.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Let’s start with simple one. I want to rewrite my URL.

Original URL:

After Rewriting:

To do this we need to tell the Apache server to internally redirect my requests to


If there is any error in .htaccess file, you get the internal server misconfiguration error with error code 500.

^ – Match starts from the beginning.
$ –  Matches the end.
? –  It makes preceding character optional.

Whenever a user enters htaccess or htacess/ in a URL, it checks the rule written in a htacces file and corresponding resource is displayed.

URL rewriting works if apache mod_rewrite module is installed and enabled. So we will write a conditional statement to check whether a mod_rewrite module is installed and enabled before URL rewriting.



$1 – is argument which is passed.

Rewrite url with three parameters

Original URL

After Rewriting:

Remove a File Extension through .htaccess

Original URL:

After Rewriting:

htaccess rule to remove .php, .html etc. file extension from url.

Disable Directory Listing Through .htaccess

To disable directory listing.

To enable directory listing.

Redirecting Error Page through .htaccess

If 404 error occurs, then redirect to page not found page.

404 is a HTTP Status code which is thrown when the resource or web page you are requested doesn’t exist.

Permanent Redirect (301) through .htaccess

1. Redirect to


NC – case insensitive.
R=301 – permanent redirect to new URL.
L – Last (Stop processing rules).


RewriteEngine On start the rewrite module.

RewriteCond %{HTTP_HOST} !^$ [NC]  - This rule fires only when the HTTP host (url) are not start with .

RewriteRule ^(.*)$$1 [L,R=301]  – It means redirect to a new domain. The R=301 means the web server returns a 301 moved permanently to the requesting browser or search engine.

2. Redirect to .

Deny Access of Files and Folders through .htaccess

Leverage Browser Caching through .htaccess


URL rewriting is the best way to rewrite ugly URL into short seo-friendly URLs. I tried my best to explain URL rewriting concept with examples. If I miss any point or you want to correct any point let me know through your comments.


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.