29

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 18

48
+150
RewriteCond %{REQUEST_URI} !^/(csNewsAd|csNewsAd/.*)$ 

instead of

RewriteRule ^/csNewsAd($|/) - [L] 
6
  • Didn't work either. Which is odd, because this should work already. I'm starting to hate Wordpress, might as well try to configure a subdomain to access this folder and get rid of the problem. Commented Feb 28, 2010 at 19:44
  • Although it didn't work, I'm gonna take this one as as accepted answer, as it is the most complete one. Commented Feb 28, 2010 at 20:21
  • I would try Wordpress official forums. I've had some strange problems in the past in this CMS and they solved. If it's not working, try to insert some garbage in your .htaccess and see if it's really working, maybe it's another problem with your installation. Good luck!
    – GmonC
    Commented Feb 28, 2010 at 20:27
  • +1 Good job. I ended looking at WP MU for a few hours before coming back and realizing I still wanted to do this.
    – phyatt
    Commented Jul 20, 2013 at 6:38
  • 1
    Please update the Source link. It leads to a German page unrelated to the question.
    – MSD
    Commented Apr 16, 2015 at 23:05
23

Place the line below in the .htaccess file in the root.

ErrorDocument 401 default

Had the exact same problem and this worked for me. It is not the problem that the redirects don't work. The problem is that the 401 (Authorization Required) error is nog defined so the "popup" doesn't show.

3
  • 3
    This is the exact answer that worked for me. I had a password-protected subdirectory that I couldn't answer when I had Wordpress installed in the root directory. Commented Mar 13, 2014 at 15:08
  • 1
    Excellent. This worked for me as well with an issue to exclude a folder so that it would return a regular 404 instead of parsing it thru wordpress. Ofcourse I changed 401 to 404. Commented May 15, 2015 at 7:49
  • This exactly what I needed too! all other's solutions just didn't work!
    – Tuhin A.
    Commented Mar 22, 2021 at 8:29
7

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>
2
  • it might have something to due with the fact that this question is 8 years old. None of the previous information here is probably useful. Commented Oct 23, 2018 at 23:22
  • Right, this code works for me inside WP htaccess and I never needed this stuff until WP decided to showme 404 not found into other php program deployed on one level up subdirectory Commented Dec 4, 2019 at 21:47
5

Was having the same issue and found the answer to my problem here: http://kb.siteground.com/article/How_to_exclude_a_folder_from_Wordpress_permalinks.html

--From the site

To exclude the subfolders from the WordPress rewrite rules, you need to edit the .htaccess file and change the bold line below:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L] # Original line
RewriteRule ./ /index.php [L] # New line
</IfModule>
# END WordPress
1
  • Tried this, but it seemed to break other non-directory-name-matching permalinks. In the end I got the code in @Joel Alejandro 's question to work as is. Could it simply be an issue that apache config needs to be reloaded? See developer.mindtouch.com/en/kb/…
    – Andy G
    Commented Jun 18, 2012 at 8:34
3

.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.

2
  • 3
    Even simply creating an empty .htaccess file in the directory you want ignored does the trick. Commented Mar 4, 2015 at 21:55
  • @AndrewTibbetts, you're right about that: didn't know that at the time. Commented Mar 7, 2015 at 0:53
2

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]
1
  • @Joel Alejandro: Did you replace your other rule with the leading /?
    – Gumbo
    Commented Feb 26, 2010 at 16:06
1

I first started doing this with Rails. At the top of the rails .htaccess are the following two lines.

# RewriteCond %{REQUEST_URI} ^/notrails.*
# RewriteRule .* – [L]

When I finally followed their example it worked.

I wanted to exclude images, javascripts, stylesheets, css, images-global, js-global (etc) so I changed the above to.

RewriteCond %{REQUEST_URI} ^(images|javascripts|stylesheets|css|images-globa|js-global|js).*
RewriteRule .* – [L]

And it worked the way I needed.

We won’t talk about how it is that I have so many different javascript, stylesheet and images folders….

But this does make my “error” file less painful. If someone adds an image file that doesn’t exist, my dispatch doesn’t have to process it.

1

Here is my solution with regard to the GoDaddy WordPress site. My install had a web.config file so I added a rewrite rule. I wanted an /ApiData path where I could run an MVC.net application (virtual directory) for AJAX calls.

<system.webServer>
    <!-- ... -->
    <rewrite>
        <rules>
            <rule name="apidata" stopProcessing="true">
                <match url="apidata"/>
            </rule>
            <rule name="wordpress" patternSyntax="Wildcard">
                <match url="*"/>
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>

        </rules>
    </rewrite>
</system.webServer>
1
  • I am replacing a DNN site in a subdirectory with a WordPress site in the root. This worked perfect so I could still see the old DNN site while I build the new site in place. Commented Aug 20, 2017 at 15:40
1

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

1
  • 1
    Welcome to Stack Overflow! Please note that when linking to your own website you should explicitly mention that it is your website, otherwise it will be removed as spam. For the moment I have removed the links, as the post can stand without them.
    – Adriaan
    Commented Feb 16, 2019 at 13:10
1

I have GoDaddy hosting, and this issue killed me. I had the same issues over and over again with conflicting .htaccess options in public_html, with my password-protected sub-directory.

I ended up buying another account because I tried about a million different modifications to my .htaccess file, and nothind worked around this problem. I'm sharing my setup and solution, now, just for completeness and clarity. (Just so you know, the account was worth it anyway, so no biggie.)

If it looks like a combination of other's answers, that's because it is. It took ALL of that to work.

Directory structure:

-otherfiles
---public_html
---.htaccess
-----subfolder
---.htaccess

The subfolder .htaccess has password the protect feature, so similar to this:

AuthType Basic
AuthName "Attendance"
AuthUserFile "/home/user/.htpasswds/public_html/attendance/passwd"
require valid-user

The public_html .htaccess originally had this, and it wasn't working (in that it swallowed EVERYTHING and redirected it, so that FatFreeFramework or CodeIgniter was handling it):

RewriteEngine On
RewriteCond $1 !^(index.php|resources|subfolder|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

In order to fix this problem, I added two lines to my public_html .htaccess file. And so you're not guessing as to where, I'll show the whole file again:

RewriteEngine On
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(subfolder|subfolder/.*)$
ErrorDocument 401 default
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
0

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?

1
  • There's another .htaccess for password protection. I'll add the code to the question. Commented Feb 24, 2010 at 16:05
0

You should try this one

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/(gadget)/(.*) /gadget/$2 [R]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
0

I'm pretty sure that you have already ruled this out, but since I came here for the same exact issue I wanted to share it with you: in my case the problem was actually on the permissions of - apparently, not being able to access the folder, the exclusion was being ignored.

0
0

It's worth noting that it matters where in the flow of the .htaccess this line goes.

See here : http://tanyanam.com/technology/wordpress-exclude-directory-from-url-rewrite-with-htaccess - the line needs to come before the WordPress ones, it won't work after them.

Rob

0

I know its an old post, but it still helped me. I had the additional problem of several blocks of RewriteRules:

##
## White listed folders
##
RewriteCond %{REQUEST_URI} !^/(phplist|phplist/.*)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
...
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]

##
## Block all PHP files, except index
##
RewriteCond %{REQUEST_URI} !^/(phplist|phplist/.*)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule !^index.php index.php [L,NC]

##
## Standard routes
##
RewriteCond %{REQUEST_URI} !^/(phplist|phplist/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

and had to add my RewriteCond before every block...

0

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]
0

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/

-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.