【发布时间】:2015-01-01 22:24:55
【问题描述】:
我在 xampp 上使用 zend 框架 2。我在 root('htdocs/zendtest/.htaccess') 中创建了 .htaccess 文件,其中包含以下代码,
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} $ [NC]
RewriteCond %{HTTP_HOST} !/zendtest
RewriteRule ^(.*)$ /zendtest/public/$1 [R=301,L]
我的 'zentest/public/.htaccess' 代码是,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
'zentest/public/index.php'代码:
<?php
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
当我在浏览器上打开“localhost/zendtest”时,它会将我带到“localhost/zendtest/public”。我想通过仅设置 htaccess 从 url 中删除“public”(这将在 localhost 和在线服务器中都有效) ),不使用虚拟主机。我该怎么做?('localhost/zendtest/'目录中没有'index.php'文件)
-谢谢。
编辑:
更新:
目录:
我在“zendtest/”文件夹中有以下 .htaccess(根据 Ravi Thapliyal 的回答)(“zendtest”文件夹中现在没有“zendtest/index.php”。只是 .htaccess。所有其他文件夹结构都是与“目录”图片相同。)
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /zendtest/
RewriteCond %{REQUEST_URI} !/public [NC]
RewriteRule ^(.*)$ public/$1 [L]
但现在在浏览器上打开'localhost/zendtest/'时出现以下问题-
已解决:
我不得不做出一些改变,如下图(.htaccess 很好):
并实现了目标-
【问题讨论】:
-
您正在使用重定向 (
[R]) 而不是内部重写(没有[R]的规则)。请注意,重定向可能会被您的浏览器缓存,因为您使用了永久重定向。这可能会弄乱您所做的测试。 -
我试过 "
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./zendtest/index. php [L] " 这也是我成功用于 codeigniter 的。但是codeigniter在zend没有的基本目录中有'index.php'。所以这显示了浏览器上的目录结构。我应该在那里创建一个“index.php”文件吗?