【问题标题】:.htaccess Add an exception to 404 redirect to particular sub-directory [closed].htaccess 将异常添加到 404 重定向到特定子目录 [关闭]
【发布时间】:2013-04-09 12:40:05
【问题描述】:

子目录是 /addon/ 它存储我的子域(也就是插件域)的所有数据。 在这个子目录中,我还存储了我的主域数据,我的主域是 website.com

RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www.)?website.com$ [NC]
    RewriteCond %{REQUEST_URI} ^/addon/(.*)$
    RewriteRule ^(.*)$ - [L,R=404]

好的,这部分完成了,如果我输入 website.com/addon/mysecondwebsite-com 我会得到 404。 如果我输入 mysecondwebsite.com,我会看到 website.com/addon/mysecondwebsite-com 的内容。

这太好了,这就是我想要的。

现在是棘手的部分,我已经将 website.com 设置为使用 website.com/addon/website-com。无需告诉我如何为 website.com 配置 /addon/website-com。我需要为配置为使用 /addon/website-com/ 的 website.com 禁用 404,但我还需要确保如果我输入 website.com/addon/website-com 该目录返回404.*

我的重写条件提到,如果我在我的主域 (website.com) 上输入 /addon/*,我将返回 404 错误。这对我的插件域非常有用,因为它们使用 /addon/ 而不是我的主域。但是我如何也阻止 /addon/* 但仍使用我的主域?

  • website.com = 404 / BAD (website.com/addon/website-com)
  • mysecondwebsite.com = 200 / 好 (website.com/addon/mysecondwebsite-com)

  • website.com/addon/mysecondwebsite-com = 404 / 好

  • website.com/addon/website-com = 404 / 好

为了让您获得完整的图片,这是我的 /public_html/ 的 .htaccess 文件

# Part one block direct access to /addon/ folder
RewriteEngine On
        RewriteCond %{HTTP_HOST} ^(www.)?website.com$ [NC]
        RewriteCond %{REQUEST_URI} ^/addon/(.*)$
        RewriteRule ^(.*)$ - [L,R=404]

# Part two using /addon/website-com/ as website.com directory
        RewriteCond %{HTTP_HOST} ^(www.)?website.com$
        RewriteCond %{REQUEST_URI} !^/addon/website-com/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /addon/website-com/$1
        RewriteCond %{HTTP_HOST} ^(www.)?website.com$
        RewriteRule ^(/)?$ addon/website-com/index.php [L]

【问题讨论】:

    标签: .htaccess redirect subdomain http-status-code-404


    【解决方案1】:

    用这个替换你的 404 处理程序:

    ErrorDocument 404 /404.shtml
    
    RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+addon/ [NC]
    RewriteRule (?!^404\.shtml$)^.*$ - [L,R=404,NC]
    

    【讨论】:

    • 谢谢!这完全符合我的要求,除了 /addon/* 的错误页面,它们不是我的典型错误页面。当有人访问 website.com/addon/* 时,如何强制显示我的自定义 404 页面...这是我得到的 404 错误; The requested URL /addon/website-com was not found on this server. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.谢谢你帮助我:)
    • 我没有在您的 .htaccess 中看到任何 ErrorDocument 行。可能你需要这样的东西:ErrorDocument 404 /404.shtml
    • 我需要离线,如果您还有其他问题,请发表评论,我会在几个小时后参加。
    • ErrorDocument 404 /404.shtml 适用于除 /addon/* 以外的所有内容,这必须是某种阻止错误 500 的块循环效果。
    • 好的,请立即查看编辑后的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2015-10-22
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多