Wednesday, January 26, 2011

htaccess rewrite rules

  • Yeah, well, assuming the comment on your question is what you in fact want, you would do something like:

    Somewhere on apache's config:

    LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
    

    On your .htaccess:

    RewriteEngine On
    RewriteRule ^/directory/$ /mypage.php?id=something [NE,PT]
    

    This would make it so that when you open http://mydomain.com/directory/ it would serve exactly the same content as http://mydomain.com/mypage.php?id=something.

    Edit: fixed typo on RewriteEngine and fixed the regexp too... was distracted.

    Hope this helps you.

    Daniel t. : this one generated internal server error.
    Khai : Do you have mod_rewrite on your apache config? Something like `LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so`. Oh I see the problem now... editing answer :P RewriteEngine not Rewire hehe!
    warren : @Khai - I could go for a "rewire" engine for apache!
    Daniel t. : when tested, removing the / preceding directory seems to work RewriteRule ^directory/$ /mypage.php?id=15 [NE,PT]
    Khai : That makes sense. Since you're putting the RewriteRule in a .htaccess file the RerwiteBase (baseline for all rewritten URLs) is automatically set to the directory where the .htaccess is. Generally I put my RewriteRules on the vhost definition and I keep the preceding / for readability instead of setting RewriteBase.
    From Khai

0 comments:

Post a Comment