【问题标题】:RewriteRule to show root重写规则以显示根
【发布时间】:2016-06-19 08:38:23
【问题描述】:

我最近完成了为我的域开发了一个新网站,今天我想通过让访问我的域根目录 example.com 的任何人都可以使用它来“实时”推送它。

但是,我不想在目录之间来回移动移动文件,以便“归档”旧站点并让新站点可用。

相反,我在.htaccess 中添加了以下 RewriteRule:

RewriteRule ^((?!(newsite)|(oldsite)).+)$ /newsite/$1 [NC,L]

我的旧网站在根目录下,而新网站在/newsite下。

现在,当我访问我的网站 example.com 时,它会正确显示目录 /newsite 下的网站内容,但不会在 URL 中显示该内容。到目前为止一切顺利。

但是,如果 URL 中提供了一个特殊的“假”目录,我现在希望旧网站可见。所以我希望example.com/oldsite 从根目录加载内容。

这是我的文件夹树的插图,以及我想要的:

root                 <--- accessible from 'example.com/oldsite'
├──index.php        
├──css               <--- accessible from 'example.com/oldsite/css'
├──js
└──newsite           <--- accessible from 'example.com'
   ├──index.php
   ├──css            <--- accessible from 'example.com/css'
   └──js             

我尝试将.htaccess 更改为:

RewriteRule ^((?=oldsite).+)$ $1 [NC,L]
RewriteRule ^((?!(newsite)|(oldsite)).+)$ /newsite/$1 [NC,L]

但这不起作用。我尝试过的其他事情在重定向循环中呈现了该站点。有没有办法做到这一点?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用这 2 个重写规则:

    RewriteEngine On
    
    # load newsite if not newsite and not oldsite
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^((?!(?:newsite|oldsite)).*)$ newsite/$1 [NC,L]
    
    # load root if request is newsite
    RewriteRule ^oldsite(/.*)?$ /$1 [L,NC]
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 2014-07-15
      • 2019-03-05
      • 2016-04-10
      • 2015-09-14
      相关资源
      最近更新 更多