Mod ReWrite -- Allow ReWrite for most pages, disable ReWrite for selected few

Posted on 2005-05-09
Last Modified: 2012-06-21
On with my Mod ReWrite adventures...

Currently, I have a dynamic page in the root of my directory (shownews.php). Most of the pages in this root level are dynamic, so I have the following rewrite in my .htaccess file:

RewriteRule ^([^/]+)\.html$ /show_news.php?kw=$1 [L]]

So a url like would really display

This works great. The problem is that I also have a contact page and a few other pages that fall victim to my mod rewrite. For example, a link to (which is a real page) would lead to My show news script then fails, because it doesn't recognize the category 'contact.'

So my question is this:
How can I add directives to my .htaccess file that will display actual pages and ignore my shownews ReWrite Rule for specific pages(e.g. Contact, About, etc.).

Any help is appreciated.

Question by:cjason
    LVL 19

    Accepted Solution

    you can use RewriteCond before RewriteRule. For example if you want to exclude /contact.html from processing by rewriterule then you should add following line before RewriteRule directive:
    RewriteCond %{REQUEST_URI} !^/contact.html
    RewriteRule ...

    You can use multiple RewriteCond directives (by default they be concatenated using logical AND but you can say that they can be concatenated using logical OR). For example if you want to disable rewrite rule for /contact.html and /about.html pages then you should use following condition:
    RewriteCond %{REQUEST_URI} !^/contact.html [OR]
    RewriteCond %{REQUEST_URI} !^/about.html
    RewriteRule ...

    or you can make complex regular expression in one RewriteCond directive:
    RewriteCond %{REQUEST_URI} !^/[contact|about].html
    RewriteRule ...
    LVL 3

    Author Comment

    Hi ramazanyich-

    I tried your suggestion, but no avail. I think I might be misunderstanding where that Rewrite condition needs to go.

    Here's my current .htaccess file:

    Options -indexes

    <Files .htaccess>
    order allow,deny
    deny from all

    RewriteEngine on

    # Block this IP
    RewriteCond %{REMOTE_ADDR} ^69\.95\.42\.202$
    RewriteCond %{REQUEST_URI} !^/errordocs/error\.html$ [NC]
    RewriteRule ^(.*) /errordocs/error.html [R]

    # This is the key script of the site
    RewriteRule ^([^/]+)\.html$ /show_news.php?kw=$1 [L]]

    So as you can see, there is another RewriteCond thrown in there that I forgot to mention. When I tried your suggestion, I was getting internal server errors. Any ideas?
    LVL 3

    Author Comment

    ramazanyich -

    Got it working. There was something wrong with one of my own ReWrite statements. Your adivce was perfect. Thanks!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
    If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now