为公用文件夹配置.htacces

时间:2017-05-13 17:12:50

标签: apache .htaccess virtualhost

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

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

2 个答案:

答案 0 :(得分:0)

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

DocumentRoot /path/to/project/public

如果您不能这样做,或者想要使用mod_rewriteproject是文档根目录,则可以重写为public

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

答案 1 :(得分:0)

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

网站就像

  • / -> /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。