.htaccess 和 WordPress:从 RewriteRule 中排除文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2322559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:35:03  来源:igfitidea点击:

.htaccess & WordPress: Exclude folder from RewriteRule

wordpress.htaccessmod-rewrite

提问by Joel A. Villarreal Bertoldi

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.

我在 WordPress 中有这个 .htaccess 文件。它位于 /public_html/(网络根目录)。我需要从重写引擎中排除一个文件夹 (csNewsAd)。我已经尝试过这个,基于另一个类似在这里的问题,但根本没有用。

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:

/csNewsAd 中还有另一个 .htaccess 用于密码保护:


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

回答by GmonC

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

instead of

代替

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

回答by Jory Hogeveen

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

将下面的行放在根目录的 .htaccess 文件中。

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.

有完全相同的问题,这对我有用。重定向不起作用不是问题。问题是 401(需要授权)错误没有定义,所以“弹出窗口”没有显示。

回答by user1389907

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

遇到了同样的问题,在这里找到了我的问题的答案: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:

要从 WordPress 重写规则中排除子文件夹,您需要编辑 .htaccess 文件并更改下面的粗线:

# 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

回答by Jeff Karney

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

这是 #1 谷歌结果和错误答案。

The correct answer is adding this beforethe Wordpress directives.

正确的答案是在 Wordpress 指令之前添加它。

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

回答by markratledge

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

.htaccess 会影响下面的所有目录,因此如果您将 .htaccess 放在 csNewsAd 中并带有所需的重写指令,它将优先于根文件。

回答by Gumbo

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 /):

如果您在 .htaccess 文件中使用 mod_rewrite,您只需要指定 URL 路径,而不需要在您的模式中使用本地路径前缀。在您没有前导的情况下/(因为您在文档 root 中/):

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

回答by Bible Arena

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 (.*)  [L]

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

https://www.drupal.org/node/30334获得

回答by TinkerTenorSoftwareGuy

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

我有 GoDaddy 托管服务,这个问题害死我了。我一遍又一遍地遇到相同的问题,因为 public_html 中的 .htaccess 选项与我的受密码保护的子目录冲突。

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

我最终购买了另一个帐户,因为我对我的 .htaccess 文件尝试了大约一百万种不同的修改,但没有解决这个问题。我现在分享我的设置和解决方案,只是为了完整性和清晰度。(只是让你知道,这个帐户无论如何都是值得的,所以没什么大不了的。)

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:

子文件夹 .htaccess 具有密码保护功能,因此类似于:

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):

public_html .htaccess 最初有这个,但它不起作用(因为它吞下了所有东西并重定向它,所以 FatFreeFramework 或 CodeIgniter 正在处理它):

RewriteEngine On
RewriteCond  !^(index.php|resources|subfolder|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?/ [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:

为了解决这个问题,我在 public_html .htaccess 文件中添加了两行。所以你不会猜测在哪里,我会再次显示整个文件:

RewriteEngine On
RewriteCond  !^(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?/ [L,QSA]

回答by Pete

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.

这是我关于 GoDaddy WordPress 网站的解决方案。我的安装有一个 web.config 文件,所以我添加了一个重写规则。我想要一个 /ApiData 路径,我可以在其中运行 AJAX 调用的 MVC.net 应用程序(虚拟目录)。

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

回答by brad tittle

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

我首先开始用 Rails 做这件事。在轨道的顶部 .htaccess 是以下两行。

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

我想排除图像、javascripts、样式表、css、images-global、js-global(等),所以我将上面的更改为。

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

我们不会谈论我有这么多不同的 javascript、样式表和图像文件夹是怎么回事……。

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.

但这确实使我的“错误”文件不那么痛苦。如果有人添加了一个不存在的图像文件,我的调度就不必处理它。