.htaccess & WordPress: Exclude folder from RewriteRule

Viewed 78202

I have this .htaccess file in WordPress. It's located at /public_html/ (web root). I need to exclude a folder (csNewsAd) from the rewrite engine. I've tried this, based from another question similar here at SO, but didn't work at all.

AddHandler x-httpd-php5 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/csNewsAd($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Any suggestions?

More data

There's another .htaccess inside /csNewsAd for password protection:


AuthName "Clasificados"
AuthType "basic"
AuthUserFile /home/ig000192/public_html/csNewsAd/.passwd
Require valid-user

18 Answers
RewriteCond %{REQUEST_URI} !^/(csNewsAd|csNewsAd/.*)$ 

instead of

RewriteRule ^/csNewsAd($|/) - [L] 

This is the #1 google result and the wrong answer.

The correct answer is adding this before the Wordpress directives.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/subdirectoryname1/(.*)$ [OR]
    RewriteCond %{REQUEST_URI} ^/subdirectoryname2/(.*)$ [OR]
    RewriteRule ^.*$ - [L]
</IfModule>

.htaccess affects all directories underneath, so if you put an .htaccess in csNewsAd with the rewrite directives you want, it will take precedence over the root file.

If you’re using mod_rewrite in a .htaccess file, you just need to specify the URL path without the local path prefix in your patterns. In your case without the leading / (as you’re in the document root /):

RewriteRule ^csNewsAd($|/) - [L]

I was having a similar problem for two sites of mine. The comments I read here didn't work for me, but I finally got this code to work:

<IfModule mod_rewrite.c>
  RewriteEngine on

  # stuff to let through (ignore)
  RewriteCond %{REQUEST_URI} "/folder1/" 
  RewriteRule (.*) $1 [L]

obtained from https://www.drupal.org/node/30334

you could add something like this:

RewriteCond %{REQUEST_URI} !^/csNewsAd 

but this should not be needed, because if csNewsAd indeed is a directory (folder) it should not be rewritten in the first place because of

RewriteCond %{REQUEST_FILENAME} !-d

are you sure there isn't anything else sitting between you and that folder, rights or (indeed) another .htaccess?

try with this single rule :D please note that dashboard is the name of the directory I wanted to exclude so change that according to your needs.

RewriteRule ^(dashboard)($|/) – [L]

This post from Lukasz worked for me. I'm using React with React Router in a subdirectory and I was getting 404 until I modified the .htaccess file to have the rewrite condition BEFORE WordPress's rewrites.

Here are the 3 lines:

RewriteCond %{REQUEST_URI} ^/MY-REACT-SUB-DIR/.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /MY-REACT-SUB-DIR/index.html [L]

and here are these 3 lines in the full .htaccess code BEFORE the WordPress rewrite:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{REQUEST_URI} ^/MY-REACT-SUB-DIR/.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /MY-REACT-SUB-DIR/index.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Be sure to read Lakasz's post as well because you may need to modify your React app as well: https://lukaszzagroba.com/how-to-deploy-a-react-app-in-a-subdirectory-on-a-wordpress-site/

It seems to be that:

ErrorDocument 401 default

At the end of .htaccess

is the correct answer. I tried a number of solutions but then had the brain wave to check it on another browser (safari) then cleared cache and hard reload of site on chrome gave positive result (amateur mistake). Darn that caching.

On Mac (in Chrome) you need to 'control' click anywhere on the page. Then choose 'inspect' this will allow you then to 'control' click on the little circular arrow (re-fresh button) and it will give options - one is 'Empty cache and hard reload'. very useful for regaining sanity.

Related