【问题标题】:how can remove '/public' from url using htaccess for localhost and host server?如何使用 localhost 和主机服务器的 htaccess 从 url 中删除“/public”?
【发布时间】: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”文件吗?

标签: .htaccess zend-framework


【解决方案1】:

更改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 中的规则。

【讨论】:

  • 我收到此错误>>“发生 404 错误页面未找到。请求的 URL 无法通过路由匹配。没有可用的异常”。我应该在 'localhost/zendtest' 目录中为此创建一个索引文件吗?
  • 您可以尝试删除public/.htaccess 吗?你至少先进入公共目录吗?
  • got "Internal Server Error 服务器遇到内部错误或配置错误,无法完成您的请求。请通过 postmaster@localhost 联系服务器管理员,告知他们发生此错误的时间,以及您在此错误之前执行的操作。有关此错误的更多信息可能在服务器错误日志中可用。此外,在尝试使用 ErrorDocument 处理请求时遇到了 500 Internal Server Error 错误。
  • 现在在public 中放置一个test.jpg 或test.html 并尝试以/zendtest/test.html 访问。如果这有效,它确认我们的重定向工作正常,我们现在需要修复框架。接下来启用public/htaccess 文件并记下错误。
  • VirtualHost 不是独立项目的任何适当解决方案,.htaccess 和更改路线可以使其成为便携式系统解决方案:)
【解决方案2】:

我所做的是为自己设置一个本地 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 文件中,但它可以更好地模拟生产环境。

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 2021-05-11
    • 2020-01-17
    • 2020-03-19
    • 2014-02-10
    • 2019-08-31
    • 2021-01-24
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多