Cyprich.com

Occasionally Relevant Insights from a Software Builder

By

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 look like http://www.website.com/about as opposed to the conventional http://www.website.com/about.html. An SEF URL in a CMS would replace something like http://www.website.com?id=34.

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 them 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. An SEF URL will shorten the URL and remove 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 =================================]

<html>
  <head>
    <title>PHP Info</title>
  </head>

  <body>
    <?php phpinfo (); ?>
  </body>
</html>

[== Code ==============================================]

 

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 place 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

[== Code ==============================================]

 

After you made the changes, you’ll need to restart Apache. If your not sure how to do this, see How to Start, Stop, and Restart Apache in Linux.  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 ===================================] 

RewriteEngine On
RewriteBase /
RewriteRule ^one.html$ two.html

[== Code ==============================================]

 

[== Code: one.html ====================================]

<html>
  <head>
    <title>One</title>
  </head>
  <body>
    <p>
      This is the number one.
    </p>
  </body>
</html>

[== Code ==============================================]

 

[== Code: two.html ====================================]

<html>
  <head>
    <title>Two</title>
  </head>
  <body>
    <p>
      This is the number two.
    </p>
  </body>
</html>

[== Code ==============================================]

One Response to Setting Up URL Rewriting for Apache in Linux

  1. medispot says:

    That is a great tip especially to those fresh to the blogosphere.
    Brief but very accurate information… Thank you for sharing this one.

    A must read article!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.