Apache mod_rewrite to return a 404 except one directory

9,093

Convert comment to CW

I got it working with

RewriteEngine On
RewriteCond %{REQUEST_URI} !^(.*)/dir1(.*)$
RewriteCond %{REQUEST_URI} !^(.*)/dir2(.*)$
RewriteRule (.*) - [R=404,L]
Share:
9,093

Related videos on Youtube

Yanis Bendahmane
Author by

Yanis Bendahmane

Updated on September 18, 2022

Comments

  • Yanis Bendahmane
    Yanis Bendahmane over 1 year

    I'm trying to configure Apache to return a default 404 page when the / of DocumentRoot is called, except for some directories in that DocumentRoot

    Unfortunately, I'm getting a 404 even for the directories that have been specified in the RewriteCond directive.

    Here is my setup :

    RewriteEngine On
    RewriteCond ${REQUEST_URI} !/dir1/
    RewriteCond ${REQUEST_URI} !/dir2/
    RewriteRule (.*) - [R=404,L]
    

    Thanks for your help !

    • Yanis Bendahmane
      Yanis Bendahmane almost 13 years
      nm, got it working with RewriteEngine On RewriteCond %{REQUEST_URI} !^(.*)/dir1(.*)$ RewriteCond %{REQUEST_URI} !^(.*)/dir2(.*)$ RewriteRule (.*) - [R=404,L]
    • mbrownnyc
      mbrownnyc almost 13 years
      In the future, this might be of use: latenightpc.com/blog/archives/2007/09/05/…