如何使用htaccess为localhost和主机服务器从url中删除'/ public'?

时间:2013-10-26 11:49:40

标签: .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]

我的'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从网址中删除'public'(这将在localhost和online server中都有效) ),不使用虚拟主机。我可以这样做吗?('localhost / zendtest /'目录中没有'index.php'文件)

-Thanks。

编辑:

enter image description here

更新

目录:

enter image description here

我在'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 /'时会出现以下问题 -

enter image description here

解决:

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

enter image description here

并实现了目标 -

enter image description here

2 个答案:

答案 0 :(得分:8)

更改htdocs/zendtest/.htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /zendtest/

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

我不确定zentest/public/.htaccess尝试做什么,但上述情况应该透明地将/zentest重写为/zentest/public。现在,这取决于public/.htaccessindex.php中的规则。

答案 1 :(得分:0)

我所做的就是设置一个本地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>

将AllowOverride设置为All并不是必需的,因为我可以将.htaccess内容复制到该Apache vhost-config文件中,但它可以更好地模拟生产环境。

相关问题