Check For Module Presence in .htaccess

By  on  

One of my favorite Apache modules is mod_rewrite. mod_rewrite allows me to manipulate page URLs so that I can search engine friendly URLs. Not every Apache server has the mod_rewrite module installed so you will want to add a conditional statement within your .htaccess file to make sure it's there.

The .htaccess

#Wordpress's .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

If the "mod_rewrite.c" module is present, that means that mod_rewrite is available and will be used. If not, the mod_rewrite code will be ignored. Using this conditional statement will prevent 500 Internal Server Errors.

Recent Features

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Display Images as Grayscale with CSS Filters

    CSS filters aren't yet widely supported but they are indeed impressive and a modern need for web imagery.  CSS filters allow you to modify the display of images in a variety of ways, one of those ways being displaying images as grayscale. Doing so requires the...

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

Discussion

  1. Jeff Hartman

    So what is the fallback for when it is not available? If you do this you need to make sure your code has a way to handle it, especially if your mod_rewrite conditions are passing get vars.

    I personally would like to know about a 500 server error so that I could fix the problem.

  2. shinzies

    Would it be fine to include this code into httpd.conf itself?
    Or is that a bad idea?

    Thanks David.

  3. shinzies

    Nope, the answer is it is bad idea because, Rewrite Base is only valid per-directory config files.

    :D

  4. how to make a link like this rerwiteRule index.php? module = article & id = 5

    thanks

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!