Setting Up URL Rewriting for Apache in Linux

Search Engine Friendly (SEF) URL’s are very common today with the widespread use of content management systems (CMS).

These are URL’s that looks like as opposed to the conventional

A SEF URL in a CMS would replace something like

The advantages of SEF over conventional URL’s is that they are shorter and easier to send to people, and they also increase the security of a web site by hiding components are being used on a page. In its webmaster guidelines, Google recommends caution in using dynamic web pages:


If you decide to use dynamic pages (i.e., the URL contains a “?” character), be aware that not every search engine spider crawls dynamic pages as well as static pages. It helps to keep the parameters short and the number of the few.

CMS users need to take extra steps for content presentation:


If your company buys a content management system, make sure that the system creates pages and links that search engines can crawl.

A CMS may generate a long and complex URL which can prevent a search engine from indexing it. A SEF URL will shorten the URL and remove them? the character which sets off warnings with web crawlers.

The first thing that you need to do is determine if mod_rewrite is installed in Apache. You can find this by using the phpinfo() function (see phpinfo.php for example). This function will display a long list of settings that PHP and Apache are using. Scroll down to apache2handler and check if  mod_rewrite is in Loaded Modules.


Code: phpinfo.php 



If the module is installed, but URL rewriting doesn’t work, you’ll need to make minor changes to the httpd.conf file located at /etc/httpd/conf/. Change AllowOverride to All (see httpd.conf below). You may need to change this in several places in the configuration file before URL rewriting works.


Code: httpd.conf


# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
#   Options FileInfo AuthConfig Limit


AllowOverride All


After you made the changes, you’ll need to restart Apache. If your not sure how to do this, Test mod_rewrite by loading the following files (.htaccess, one.html, two.html) on your web server. The RewriteBase setting in .htaccess should point to the subfolder in your web server (i.e. /rewrite/) if the default setting below doesn’t work. When you browse to one.html, you should be redirected to two.html.

You can download the code below here.


Code: .htaccess



Code: one.html 



This is number one.



Code: two.html



This is number two.