RSS

.htaccess - mod_rewrite Tutorial

Mon, Sep 29, 2008

.htaccess

In this tutorial I will show you some simple .htaccess features. In this case mod_rewrite.

You may see on some websites, for example this one, where the URLs are very tidy and there is nothing like page.php?content=home it would just be page/home/. This is usually done through .htaccess, and it’s a lot more simple than you think.

First thing you need to do is create a file called “.htaccess” (without the quotes). This file actually has no name, its just a file extention. But thats a bit irrelivant. Also, its probably worth mentioning that you will not be able to create file on windows. You will have to create it on your web server.

Second thing to do is open it up then paste this code in it:

RewriteEngine On

What this does is allows us to do mod_rewrite. Then we need to add some rules. Firstly lets assume you have a file called ‘index.php’ and we want to rename that to ‘home’. You would add this code below the code we already have:

RewriteRule ^home$ index.php

So our full code is…

RewriteEngine On
RewriteRule ^home$ index.php

You can add as many rules as you like. There are more efficient ways of doing this but if you have never used .htaccess before its probably better to do this way, as its more simple.

Thanks for reading. :)

Rating: 0.0/10 (0 votes cast)
Share and Enjoy:
  • Digg
  • Google
  • Technorati
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Furl
  • Live
  • Reddit
  • Spurl

  • Subscribe
    • XML
    • Google Reader or Homepage
    • Add to My Yahoo!
    • Subscribe with Bloglines
    • Subscribe in NewsGator Online
    • BittyBrowser
    • Add to My AOL
    • Convert RSS to PDF
    • Subscribe in Rojo
    • Subscribe in FeedLounge
    • Subscribe with Pluck RSS reader
    • Solosub
    • MultiRSS
    • R|Mail
    • Rss fwd
    • Blogarithm
    • Eskobo
    • gritwire
    • BotABlog
    • Simpify!
    • Add to Technorati Favorites!
    • Add to netvibes
    • Add this site to your Protopage
    • Subscribe in NewsAlloy
    • Subscribe in myEarthlink
    • Add to your phone
    • Get RSS Buttons

, ,



This post was written by:

admin - who has written 41 posts on Unreal Media.


Contact the author

4 Comments For This Post

  1. zekel Says:

    one little correction.

    You can create a .htaccess file in Windows. If you in notepad save a file as “.htaccess” with the quotation marks you will be able to create the file in Windows.

    //zekel

    PS. I think you might have a small bug in your code. “PHP parsed this page in 1222979795.48 seconds.”, 1.2 billion seconds is quite a long time. DS.

    Rating: 0.0/5 (0 votes cast)
  2. zekel Says:

    ops, forgot to say thank you.

    //zekel

    Rating: 0.0/5 (0 votes cast)
  3. Jay Says:

    Zekel is right, it can be created on Windows.

    @Zekel: I emailed him about that. :P I thought it was funny, it took a few decades to load the page. XD

    Another common use for mod_rewrite, besides hiding query strings, is to keep a user at either http://www.domain.com or just domain.com. Some people don’t like the http://www., so they force the user to use just the domain. Others do the exact opposite and always keep the http://www. in the URL.

    Here’s the rewrite code to do this, it will redirect a user coming to http://www.domain.com to domain.com.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/1 [R=301,L]

    (It should be noted that the above only works when using HTTP. A bit of alteration would be required to allow this to work with HTTPS aswell.

    Rating: 0.0/5 (0 votes cast)
  4. uzscxiynkb Says:

    K94yk4 mloznzztvrug, [url=http://aennzethewxo.com/]aennzethewxo[/url], [link=http://ylqnvpswyjar.com/]ylqnvpswyjar[/link], http://isdltgyjohey.com/

    Rating: 0.0/5 (0 votes cast)

Leave a Reply