Setting Up URL Rewriting for Apache in Linux

Posted by Johan Cyprich on 17 Aug 2010 | Tagged as: How To

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 ==============================================]



Tweet This Tweet This Post!

Related posts:
    Display a List of Installed Modules in Apache
    How to Start, Stop, and Restart Apache in Linux
    Managing Linux with Webmin
    Using .htaccess in Windows

Share this post:

del.icio.us:Setting Up URL Rewriting for Apache in Linux digg:Setting Up URL Rewriting for Apache in Linux spurl:Setting Up URL Rewriting for Apache in Linux wists:Setting Up URL Rewriting for Apache in Linux simpy:Setting Up URL Rewriting for Apache in Linux newsvine:Setting Up URL Rewriting for Apache in Linux blinklist:Setting Up URL Rewriting for Apache in Linux furl:Setting Up URL Rewriting for Apache in Linux reddit:Setting Up URL Rewriting for Apache in Linux fark:Setting Up URL Rewriting for Apache in Linux blogmarks:Setting Up URL Rewriting for Apache in Linux Y!:Setting Up URL Rewriting for Apache in Linux smarking:Setting Up URL Rewriting for Apache in Linux magnolia:Setting Up URL Rewriting for Apache in Linux segnalo:Setting Up URL Rewriting for Apache in Linux gifttagging:Setting Up URL Rewriting for Apache in Linux

Follow Me:

Did you find this post interesting and useful? You can keep up to date on this blog by subscribing to my RSS feed, or you can have new posts sent to you by e-mail. You can also follow me on Twitter.


Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply

CommentLuv badge