【问题标题】:Configure .htacces for public folder为公用文件夹配置 .htaccess
【发布时间】:2017-10-12 20:15:14
【问题描述】:

我在一个 php 项目中有以下文件夹结构: project/ |--app/ |--public/ |--img/ |--.htaccess |--index.php |--vendor/ .htacces 文件将如何从host.domain/project/ 提供public 文件夹?

我猜 .htaccess 需要在根文件夹中。我还可以编辑host.domain 的服务器虚拟主机文件,但我不知道配置如何。

【问题讨论】:

    标签: apache .htaccess virtualhost


    【解决方案1】:

    如果您有权访问主/虚拟主机配置,则可以将DocumentRoot 设置为public 文件夹

    DocumentRoot /path/to/project/public
    

    如果你不能这样做,或者想使用mod_rewrite 并且project 是文档根,你可以重写public

    RewriteCond %{REQUEST_URI} !^/public
    RewriteRule ^(.*)$ /public/$1 [L]
    

    【讨论】:

    • 不,这不起作用。这可能是因为公用文件夹也有一个 .htaccess。我试过:RewriteCond %{REQUEST_URI} !^/public RewriteRule ^(.*)$ /public/$1 [L]RewriteCond %{REQUEST_URI} !^/public RewriteRule ^(.*)$ public/$1 [L]RewriteCond %{REQUEST_URI} !^/public RewriteRule ^(.*)$ public/index.php/$1 [L] 和更多类似的变体。我将不得不等待获取域或子域,然后添加 VirtualHost 配置。
    • @Jedabero 你得到什么错误? /public/.htaccess 长什么样子?
    【解决方案2】:

    以下是如何将公用文件夹系统与旧系统混合使用

    网站是这样的

    • / -> /public/index.php?route=/
    • /mypage -> /public/index.php?route=/mypage
    • /fakefolder/mypage -> /public/index.php?route=/fakefolder/mypage
    • /fakefolder/mypage?a=b&foo=bar -> /public/index.php?route=/fakefolder/mypage&a=b&foo=bar(因为 Apache2 的 QSA 标志有效)

    /public/ 中的所有静态文件都可以访问,例如 /assets/img/foo.png 位于文件系统上的 /public/assets/img/foo.png

    管理系统以“旧方式”工作

    • /admin/ -> /admin/index.php
    • /admin/login -> /admin/login.php
    RewriteEngine on
    
    # Already in admin folder, use admin folder, old routing system
    RewriteCond %{REQUEST_URI} ^/admin
    # It is a folder, do not re-write it
    RewriteCond %{REQUEST_FILENAME} !-d
    # Do not hit this once more if it already is a file, .php was already added
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^admin/(.*)$ /admin/$1.php [L,QSA]
    
    # See: https://stackoverflow.com/a/43957084/5155484
    # Not already in public folder or admin folder, use public folder
    RewriteCond %{REQUEST_URI} !^/public
    RewriteCond %{REQUEST_URI} !^/admin
    RewriteRule ^(.*)$ /public/$1 [L]
    
    
    # Use router if it was redirected by previous pass to public folder
    # And it is not a file, that way files are served as static files
    RewriteCond %{REQUEST_URI} ^/public
    # Do not hit this once more if it already is a file, .php was already added
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^public/(.*)$ /public/index.php?route=/$1 [L,QSA]
    

    如果在某些生产服务器上将RewriteRule/ 更改为./,则会出现错误500。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-27
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多