【问题标题】:RewriteCond checking file existence on parent directoryRewriteCond 检查父目录上的文件是否存在
【发布时间】:2013-06-04 17:53:26
【问题描述】:

如果请求的文件或目录不存在,我会尝试将所有请求重定向到脚本,但我需要改进此 .htaccess 以检查文件是否存在于父文件夹中。
我目前有:

RewriteEngine on

# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# map requests to contentpage.php and appends it as a query string
RewriteRule ^(.*)/(.*)$ contentpage.php?url=$2&lang=$1

现在,这个 .htaccess 成功重定向
en/products.php -> contentpage.php?url=products.php&lang=en only 如果 products.php 在子目录 /en/ 上不存在(实际上目录 /en/ 并不存在,因此当我有文件时 RewriteCond 无法过滤products.php 在父目录上)。

我需要检查文件是否存在于我的文件所在的顶级目录中。它只需要重定向语言目录,例如 /en /es /de /pt .etc

有人知道如何解决这个问题吗?谢谢

【问题讨论】:

    标签: php apache .htaccess parent


    【解决方案1】:

    您可以将您的规则放在您的顶级目录中的 htaccess 文件中;这样,如果你有

     /parentdir
         /en
         /es 
         /de
    

    即使您访问父路径或子路径:

    • /parentdir/products.php
    • /parentdir/en/products.php

    评估 /parentdir/.htaccess 规则
    所以你可以使用这样的规则集:

    # check if requested file does not exists in the child dir
    # check if requested file does not exists in the child dir
    # if does not exists... check if it also does not exists in the parent dir
    RewriteCond /parentdir/$1/$2 !-f
    RewriteCond /parentdir/$2 !-f
    RewriteRule ^/parentdir/(es|en|fr|de)/(.*)$ contentpage.php?url=$2&lang=$1
    
    # check if requested file does not exists in the parent dir
    RewriteCond /parentdir/$1 !-f
    RewriteRule ^/parentdir/(.*)$ contentpage.php?url=$2&lang=$1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-16
      • 2015-03-21
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多