.htaccess 如何删除'/公共';从url使用htaccess访问本地主机和主机服务器?

.htaccess 如何删除'/公共';从url使用htaccess访问本地主机和主机服务器?,.htaccess,zend-framework,.htaccess,Zend Framework,我正在xampp上使用zend framework 2。我在根目录中创建了.htaccess文件('htdocs/zendtest/.htaccess'),其中包含以下代码 Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} $ [NC] RewriteCond %{HTTP_HOST} !/zendtest RewriteRule ^(.*)$ /zendtest/public/$1 [R=301,L] 我的'z

我正在xampp上使用zend framework 2。我在根目录中创建了.htaccess文件('htdocs/zendtest/.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/”时出现以下问题-

已解决:

我必须做一些更改,如下图所示(htaccess很好):

实现了这个目标-


更改
htdocs/zendtest/.htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /zendtest/

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

我不确定zentest/public/.htaccess试图做什么,但上面的内容应该透明地重写为
/zentest
。现在,这取决于您在
public/.htaccess
和那里的
index.php
中的规则。

我要做的是为自己设置一个本地Vhost,可以通过Firefox访问。 为此,我配置Vhost并操作/etc/hosts文件

<VirtualHost *:80>
ServerName p120.local
ServerAdmin webmaster@localhost
DocumentRoot //var/www/tokam_dev/p120/public
        <Directory /var/www/tokam_dev/p120/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
SetEnv APPLICATION_ENV "development"
RewriteLog /tmp/p120-rewrite.log
</VirtualHost>

服务器名p120.local
服务器管理员webmaster@localhost
DocumentRoot//var/www/tokam_dev/p120/public
选项索引跟随符号链接多视图
允许超越所有
命令允许,拒绝
通融
SetEnv应用程序_ENV“开发”
RewriteLog/tmp/p120-rewrite.log

将AllowOverride设置为All并不是真正必要的,因为我可以将.htaccess内容复制到Apache vhost配置文件中,但它更好地模拟了生产环境

您正在使用重定向(
[R]
)而不是内部重写(没有
[R]
的规则)。请注意,您的浏览器可能会缓存重定向,因为您使用了永久重定向。这可能会打乱您的测试。我尝试了“RewriteEngineonRewritebase/RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_FILENAME}!-重写规则^(.*$./zendtest/index.php[L]”,我也成功地将其用于codeigniter。但codeigniter在基本目录中有'index.php',zend没有。这显示了浏览器上的目录结构。我应该为此创建一个'index.php'文件吗?我得到了这个错误>>“一个404错误发生了,找不到页面。请求的URL无法通过路由匹配。没有异常可用”。我应该在“localhost/zendtest”目录中为此创建一个索引文件吗?您可以尝试删除
public/.htaccess
吗?你至少要先进入公共目录吗?明白了吗“内部服务器错误服务器遇到内部错误或配置错误,无法完成您的请求。请通过以下地址与服务器管理员联系:postmaster@localhost通知他们此错误发生的时间,以及您在此错误之前执行的操作。有关此错误的详细信息,请参阅服务器错误日志。此外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误。“现在将test.jpg或test.html放在
public
中,并尝试以
/zendtest/test.html
的身份访问。如果这起作用,它确认我们的重定向工作正常,我们现在需要修复框架。接下来启用
public/htaccess
文件并记下错误。VirtualHost不是独立项目的合适解决方案。htaccess和更改路由可使其成为便携式系统解决方案:)
<VirtualHost *:80>
ServerName p120.local
ServerAdmin webmaster@localhost
DocumentRoot //var/www/tokam_dev/p120/public
        <Directory /var/www/tokam_dev/p120/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
SetEnv APPLICATION_ENV "development"
RewriteLog /tmp/p120-rewrite.log
</VirtualHost>