.htaccess & WordPress: Exclude folder from RewriteRule

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

Upvotes: 29

Views: 83975

Answers (18)

sigmapi13
sigmapi13

Reputation: 2533

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/

Upvotes: 0

Hugh Brown
Hugh Brown

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

Upvotes: -1

jerryurenaa
jerryurenaa

Reputation: 4704

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]

Upvotes: 0

TinkerTenorSoftwareGuy
TinkerTenorSoftwareGuy

Reputation: 797

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]

Upvotes: 1

Bible Arena
Bible Arena

Reputation: 11

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

Upvotes: 1

Jeff Karney
Jeff Karney

Reputation: 71

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>

Upvotes: 7

user783388
user783388

Reputation:

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

Upvotes: 0

GmonC
GmonC

Reputation: 10970

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

instead of

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

Upvotes: 48

Pete
Pete

Reputation: 2463

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>

Upvotes: 1

rob_was_taken
rob_was_taken

Reputation: 413

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

Upvotes: 0

Jory Hogeveen
Jory Hogeveen

Reputation: 330

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.

Upvotes: 23

TheLegs
TheLegs

Reputation: 1

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.

Upvotes: 0

Preetam Purbia
Preetam Purbia

Reputation: 5922

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

Upvotes: 0

user1389907
user1389907

Reputation: 51

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

Upvotes: 5

brad tittle
brad tittle

Reputation: 11

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.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655269

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]

Upvotes: 2

futtta
futtta

Reputation: 5920

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?

Upvotes: 0

markratledge
markratledge

Reputation: 17561

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

Upvotes: 3

Related Questions