.htaccess & Wordpress: Exclude folder from Rewrite

2019-01-06 17:42发布

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

14条回答
做自己的国王
2楼-- · 2019-01-06 17:52

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>
查看更多
【Aperson】
3楼-- · 2019-01-06 17:55

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

查看更多
混吃等死
4楼-- · 2019-01-06 17:57

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]
查看更多
Emotional °昔
5楼-- · 2019-01-06 17:57

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?

查看更多
Ridiculous、
6楼-- · 2019-01-06 18:01

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]

So, that's my story. It works now. Thank you to Jory and GmonC!!! I upvoted you guys.

查看更多
可以哭但决不认输i
7楼-- · 2019-01-06 18:05

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
查看更多
登录 后发表回答