【问题标题】:Redirect entire subdirectory to a domain将整个子目录重定向到域
【发布时间】:2018-03-31 05:42:13
【问题描述】:

我正在尝试将整个子目录重定向到我们的主域。

基本上是这样的链接:

  • https://garrysun.com/dev/ayurveda-products/categories/ayurvedic-ghee-clarified-butter?limit=15
  • https://garrysun.com/dev/
  • https://garrysun.com/dev/ayurveda-products/

应该都去

https://garrysun.com/

我们正在使用 OpenCart 2.3.0.2

我试过了:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/dev/(.*)$
RewriteRule ^(.*) https://garrysun.com/ [R=301,NC]

RewriteRule ^dev/(.*)$ https://garrysun.com [R=301,NC,L]

但似乎两者都不起作用。完成这项工作的最佳 .htaccess 规则是什么?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    /dev/.htaccess 内部有这条规则:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?garrysun\.com$ [NC] 
    RewriteRule ^ https://garrysun.com/? [R=301,L]
    

    根据响应标头,您的 Web 服务器似乎是 Nginx。对于 Nginx,请使用此规则:

    location ~ ^/dev/ {
        return 301 https://garrysun.com/?;
    }
    

    【讨论】:

    • 所以我在 dev 文件夹中除了 .htaccess 文件之外什么都没有,只有上面显示的行。如果我转到garrysun.com/dev,它会将我重定向到主站点,但如果我转到另一个页面,我会收到 404 错误:garrysun.com/dev/ayurveda-products
    • 抱歉,我尝试了其他导致 403 错误的方法。现在是 404。
    • 可能是因为它试图转到网络根目录而不是garrysun.com
    • 我在garrysun.com/dev 上得到403,在garrysun.com/dev/ayurveda-products 上得到404
    • 响应标头显示server: nginx/1.12.2,我仍然得到https://garrysun.com/dev/ 的403,这意味着上面的.htaccess 甚至没有被读取。
    【解决方案2】:

    我使用了这个重定向:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)$ /$1 [R=301,L]
    

    问题是,如果该页面在主网站中不存在,我会收到 404 错误。

    例如: 这个:https://garrysun.com/dev/777-oil-psoriasis 变成这样:https://garrysun.com/777-oil-psoriasis 我收到 404 错误,因为该页面在主站点上不存在。

    这有效: https://garrysun.com/dev/ayurveda-products/ 变成: https://garrysun.com/ayurveda-products/ 因为新网站有那个页面。

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多