【问题标题】:rewrite rule to remove a folder from a path and keep rest of directory structure重写规则以从路径中删除文件夹并保留其余目录结构
【发布时间】:2012-09-19 22:40:42
【问题描述】:

在我的基本目录中,我有很多文件夹。我想把这些文件夹放到一个子目录中。

domain.com/test/test.html

会有文件结构:

domain.com/subdir/test/test.html

并且仍然在同一个网址:domain.com/test/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

从此处复制的另一个答案可以做到这一点,但它也会停止对基本目录的其他请求。

我需要它来检查子目录中的文件夹名称是否匹配,以及它是否存在或不从基本目录提供服务。

【问题讨论】:

    标签: .htaccess url mod-rewrite directory structure


    【解决方案1】:

    试试这个:

    RewriteEngine on
    
    # these conditions are optional, they're to make sure you don't clobber a legit request
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # these conditions aren't optional, they check to see if the requested file/dir exists inside /subdir
    RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -d
    
    # rewrite
    RewriteRule ^(.*)$ /subdir/$1 [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多