• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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

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 http://www.domain.com/usa.html would really display http://www.domain.com/shownews.php?kw=us.

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 http://www.domain.com/contact.html (which is a real page) would lead to http://www.domain.com/shownews.php?kw=contact. 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.

  • 2
1 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 ...
cjasonAuthor Commented:
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?
cjasonAuthor Commented:
ramazanyich -

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now