Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:09:14+00:00 2026-05-25T11:09:14+00:00

In many rewrite rule answers, when testing the url for a certain condition, I

  • 0

In many rewrite rule answers, when testing the url for a certain condition, I often see a mix of using RewriteCond with REQUEST_URI, and the RewriteRule itself.

Is this just personal preferences, or is there a performance reason, or just clarity of the rules? All of these are valid reasons, in my opinion; I’m just wondering if there’s a particular reason.

I know there are conditions where RewriteCond is the only choice. I’m interested here in the cases where the RewriteRule would also work. Generally these are simpler rules.

Here are some examples:

EXAMPLE 1

This answer has a common pattern, allow certain folders as-is.
Htaccess maintenance mode allow certain directories

# always allow these folders
RewriteCond %{REQUEST_URI} ^/display_me_always [OR]
RewriteCond %{REQUEST_URI} ^/another_folder [OR]
RewriteCond %{REQUEST_URI} ^/even_more_folders
RewriteRule .+ - [L]

This could be done with just a RewriteRule as:

RewriteRule ^/(?:display_me_always|another_folder|even_more_folders) - [L]

(Added the ?: for non-capturing. I’m never quite sure if it’s faster to have a simpler rule, or not to capture.)

EXAMPLE 2

Modifying this for a more common scenario, redirect certain folders to other folders.

RewriteCond %{REQUEST_URI} ^/display_me_always [OR]
RewriteCond %{REQUEST_URI} ^/another_folder [OR]
RewriteCond %{REQUEST_URI} ^/even_more_folders
RewriteRule ^/[^/]+/(.*)$ /other_location/$1 [L]

This seems simpler with the rule only.

RewriteRule ^/(?:display_me_always|another_folder|even_more_folders)/(.*)$ /other_location/$1 [R=301,NC]

EXAMPLE 3

This answer has a common pattern, redirect if not already in the target location.
Mod Rewrite rule to redirect all pdf request to another location. Test the folder with the RewriteCond, then test the file with the rule.

I can see a negative condition being much clearer to do with RewriteCond, but it’s still possible with RewriteRule.

RewriteCond %{REQUEST_URI} !^/web_content/pdf/
RewriteRule ^(.+\.pdf)$ /web_content/pdf/$1 [L]

This could be written as

RewriteRule ^(?!/web_content/pdf/)(.+\.pdf)$ /web_content/pdf/$1 [L]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:09:15+00:00Added an answer on May 25, 2026 at 11:09 am

    One simple reason: it’s much easier to understand for a non regex guru.

    The rewrite rule that you will give to a OP today, he may need to modify one day later .. and the easier the rule to understand the more chances that he will look into it himself and not running again to this place for yet another small fix / new similar rule.

    Yes, combined rule is faster — no doubts here. But the time spent at URL rewriting is still so small compared to a single script execution … that it only can make some difference on very-very CPU busy servers and when there are a lot of such rewrites.

    Therefore, the most optimal will be something in between — which is still easy to read and is compact and efficient at the same time. Instead of

    # always allow these folders
    RewriteCond %{REQUEST_URI} ^/display_me_always [OR]
    RewriteCond %{REQUEST_URI} ^/another_folder [OR]
    RewriteCond %{REQUEST_URI} ^/even_more_folders
    RewriteRule .+ - [L]
    

    offer

    # always allow these folders
    RewriteCond %{REQUEST_URI} ^/(display_me_always|another_folder|even_more_folders)
    RewriteRule .+ - [L]
    

    You cannot become a master in a day or two (unless, maybe, you are some kind of genius) — everything takes time. And the more practical experience you have (by modifying these rules yourself) the better it is for you to move further, to produce more efficient/stable rule.

    BTW:
    This rule will not work if placed in .htaccess (URL in matching pattern starts with no leading slash):

    RewriteRule ^/(?:display_me_always|another_folder|even_more_folders) - [L]
    

    But will work fine if placed in server config / virtual host context — that’s one of the “nuances” you need to know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following code: RewriteCond %{REQUEST_URI} !assets/ RewriteCond %{REQUEST_URI} !uploads/ RewriteRule ^([a-z|0-9_&;=-]+)/([a-z|0-9_&;=-]+)
I found this code to force using www. RewriteEngine On RewriteCond %{HTTP_HOST} !^www.example.com$ RewriteRule
Is there any one know, what is the url rewrite rule in wikipedia article's
I have this rewrite rule RewriteEngine On RewriteBase /location/ RewriteRule ^(.+)/?$ index.php?franchise=$1 Which is
I'm looking for an SEO friendly url rewrite rule that would work for any
How come this one works: RewriteEngine On RewriteCond %{REQUEST_URI} ^/book/blabla$ RewriteRule ^.+$ /book/?name=blabla [NC,L]
The IIS URL Rewrite Module ships with 3 built-in functions : * ToLower -
I need a rewrite rule that will do an internal redirect from: <domain>/directory/<anything>/<anything>.php to:
I have the following rule rule in my .htaccess file RewriteRule dest/(.*)/item/(.*)/aId/(.*)/tId/(.*)/? rss.php?dest=$1&item=$2&aId=$3&bId=$4 [NC,L]
1) I would like to create a rewrite rule that removes the file at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.