【问题标题】:How to block access to all pages of the website except /blog in .htaccess?如何阻止对.htaccess 中除/blog 之外的网站所有页面的访问?
【发布时间】:2020-11-20 15:21:55
【问题描述】:
RedirectMatch 404 ^/$

此代码阻止主页

RedirectMatch 404 ^/.*$

此代码阻止所有页面

如何得到除一个(/blog)以外的所有页面都被屏蔽(404)的效果?

【问题讨论】:

  • 您能否通过编辑您的问题来显示您的完整 .htaccess
  • 您是否特别想为此使用 mod_alias?

标签: .htaccess mod-alias


【解决方案1】:

这应该可行:

RedirectMatch 404 ^/((?!blog).*)$

【讨论】:

  • 这段代码阻止了一切:主页、子页面和 /blog...
  • 哦,是的,对不起,这将阻止所有不是以/blog开头的页面
  • @TP999 由于/blog 被阻止,您可能与现有指令有冲突。 (?)
  • @TP999 此重定向会阻止除 /blog/blog/foobar 之外的所有内容。它在您的服务器上不起作用的原因是因为您正在使用RewriteRules 来处理其他 URL,并且该规则与该规则冲突。如果您想解决此问题,请使用RewriteRule。而不是 RedirectMatch 。 @anubhava 的解决方案绝对适合您。
【解决方案2】:

如何得到除一个(/blog)以外的所有页面都被屏蔽(404)的效果?

您可以使用:

RewriteEngine On
RewriteBase /

# if request doesn't start with /blog then return R=404
RewriteCond %{THE_REQUEST} !\s/+blog[\s?] [NC]
RewriteRule ^ - [L,R=404]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

【讨论】:

  • 不幸的是它不起作用。在我的 wordpre 上输入此代码后,错误 404 显示所有页面。 (如果我把“博客”这个词放在那里或其他任何东西都没有关系)。仅在删除此部分后: RewriteCond %{THE_REQUEST} !\s/+blog[\s?] [NC] RewriteRule ^ - [L,R=404] 所有页面都开始加载
  • example.com/blog 是否也返回 404?
  • 是的,我用 wordpress 在 2 个域上进行了测试,并且到处都一样,所有页面都被阻止,包括 /blog
  • 你能展示你的完整 .htaccess 吗?还有其他的 .htaccess 吗?
【解决方案3】:

我的完整 .htaccess:

RewriteEngine On
RewriteBase /

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

RedirectMatch 404 ^/((?!blog).*)$

这段代码阻止了一切:主页、子页面和/blog。

【讨论】:

    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多