more on portfolio









August 2008
M T W T F S S
« May «-»  
 123
45678910
11121314151617
18192021222324
25262728293031






 


How to: Hide file extension using Mod Rewrite


Mod Rewrite is one of the few solutions which we use to avoid spammers and SQL injection from taken place. And today, I’m going to share with you a few tips and tricks on it. Credits goes to Roshan for an awesome tutorial on his website.

What do we need for Mod Rewrite to work?
  • Mod_rewrite module loaded on the Apache server.
  • A .htaccess file created on your server root.

  • To check whether mod_rewrite is loaded…
  • Create a file called phpinfo.php on your server.
  • Type <?php phpinfo(); ?> on it and run the file.
  • Hit CTRL-F, type mod_rewrite on the search field and click submit.
  • If there is a result, then you’re SAFE! Otherwise, you have to contact your hosting provider to enable it for you.


  • How do you change PHP extension to another format?
    For instance, you want to change PHP to HTML, then copy the coding below and paste it on your .htaccess file.

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.html$ $1.php [nc]

    Take note of the one in REDS cause that is small piece of area that did the rewriting. You can change it to a different extension based on your needs from .htm to .asp. As long as it’s a web format, it will work nicely.

    Another trick I’ve tested earlier is that you can also permanently remove the extension and replace it with just a slash. Example is the url show in the address bar when you are viewing DSDexigns website. It looks more like a ‘folder’ url more then a file url.

    Example:

    http://www.dsdexigns.com/your-file-name/

    Just in case you are not sure what to replace the REDS,..

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\/$ $1.php [nc]
    


    On Roshan article, he’d also explained on how to rewrite the URL show below using Mod Rewrite…

    http://www.dsdexigns.com/file.php?cat=1&something-else=the-value


    …and it will be a little confusing if you don’t understand the logic it. Which is why there is an online URL Rewriting Tool like WebConfs to help you out with.

    Experimenting:
    I’ve did the testing earlier, and noticed that to link from one file to another, the HREF link must also be in html (or whatever you’ve rewrite it to) instead of it’s original file extension.

    If you use this method to link to another file…

    <a href="result.php">Click for Results</a>


    …I’ve noticed that the mod rewrite will not take place, and it will eventually revealed the actual file extension. I’m not sure if it is just me that is having this issue but you can test it out and see for yourself.

    And so, if you are going to link one file to another, you have to use the extension accordingly to the one marked REDS stated earlier.

    <a href="result.htm">Click for Results</a>


    Conclusion:

    In most cases it will work well, but I’m having a few doubts when FORM tag is in use. I did not have much time to fool around with the Mod Rewrite code but you can always test it out at a trial and error situation.

    For more information and related topic about Mod Rewrite, have a read at Wikipedia via http://en.wikipedia.org/wiki/Mod_rewrite.






    Leave a Reply