【问题标题】:htaccess redirect from subdomain to domain custom 404 pagehtaccess 从子域重定向到域自定义 404 页面
【发布时间】:2020-09-30 07:54:29
【问题描述】:

我有一个域和子域。我在 example.com 上创建了一个自定义 404 页面,当用户点击子域上未找到的页面时,我可以专门使用它。

例如:

当用户在 dev.example.com 上搜索一个页面时,他看到的是 example.com 主页,因为它是一个 cname,例如域。但是,当用户搜索页面并在子域 dev.example.com/blah 上收到 404 时,他应该收到专门为子域构建的自定义 404 页面,而不是 domain.com 的 404 页面。我怎样才能做到这一点

这是我在 .htaccess 中写的,但它不起作用

  RewriteCond %{HTTP_HOST} ^dev.example.com/%{REQUEST_URI} [NC]
  RewriteRule  https://example.com/custom_404 [L,R=301]

我没有在更大程度上使用 .htaccess,我正在探索,任何建议都会有所帮助。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    以下是如何在子域中显示自定义 not_found 页面的内容:

    RewriteCond %{HTTP_HOST} ^dev\.example\.com$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/custom_404$ [NC]
    RewriteRule ^ /custom_404 [L]
    

    【讨论】:

    • 感谢@anubhava,我在尝试此操作后收到内部服务器错误,我在 custom_404 路径中使用了自定义 404 路径。
    • 不幸的是,我将无法共享 htaccess,但我已将此代码写在下面,但我已将其写在 下面。调试这个的更好方法是什么
    • 如果您将此 .htaccess 重命名为其他名称,http://dev.example.com/custom_404 是否可以在浏览器中正常加载?
    • 是的,文件更改时会加载
    • 好的,试试我更新的答案。我只是猜测问题,因为我无法查看您的所有规则。
    【解决方案2】:
    Got it to work as expected, Thanks to Apache docs and @Anubhav.
    
     `
     RewriteCond %{HTTP_HOST} ^test.example.com
     RewriteCond %{REQUEST_URI} ^/log-in
     RewriteRule ^ https://test.example.com/custom_404 [NC,L]
    `
    
     1. Checks if the host is the desired subdomain,
     2. Checks if the REQUEST_URI matches /log-in. One key thing over here is the / as its mandatory for a REQUEST_URI. Using ^ means that the initial part of the string is matched. some working examples ie: /log-in, /log-in/blah, /log-in-anything 
     3. Rewrites and redirects to the custom_404 page
     
    References:
    
    https://httpd.apache.org/docs/2.4/rewrite/intro.html
    https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond
    https://httpd.apache.org/docs/2.4/rewrite/flags.html
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 2011-04-29
      • 2010-11-22
      • 2023-03-17
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多