代码之家  ›  专栏  ›  技术社区  ›  MultiDev

将所有请求重定向到目录的htaccess规则

  •  1
  • MultiDev  · 技术社区  · 6 年前

    我的文件结构如下:

    app
      /some
      /private
      /app
      /directories
    public
      /assets
        /images
        /js
        /css
      index.php (app entry point)
      robots.txt
    

    如果有人试图访问任何东西(现有的或不存在的) /app/* 请求将被重新路由到 /public/index.php .

    如果有人试图访问 /public 目录,他们可以。

    其他的都被传送到 /公共/index.php .

    以下是我所拥有的:

    # If not an existing file
    RewriteCond %{REQUEST_FILENAME} !-f 
    
    # Allow access to the public directory
    RewriteRule ^(public)($|/) - [L]
    
    # Redirect everything to this file
    RewriteRule ^ public/index.php [QSA,L]
    
    RewriteBase /
    

    这对我来说非常有用,但我需要做的是,如果文件存在于 public 目录,我希望能够这样访问它:

    site.com/robots.txt
    site.com/images/image.jpg
    

    现在,我必须使用:

    site.com/public/robots.txt
    site.com/public/images/image.jpg
    

    如何修改这些规则,以便在 公众的 不使用 公共 在路径/url中?

    1 回复  |  直到 6 年前
        1
  •  0
  •   anubhava    6 年前

    您可以在站点根目录中使用这些规则。htaccess:

    RewriteEngine On
    
    # If file exists in public directory then rewrite to public/...
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
    RewriteRule ^(.+?)/?$ public/$1 [L]
    
    # Redirect everything to this file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ public/index.php [L]