【问题标题】:how to remove public and index.php from ZF2如何从 ZF2 中删除 public 和 index.php
【发布时间】:2014-03-19 16:20:12
【问题描述】:

我想从我的网址中删除公用文件夹和 index.php。

示例 我当前的网址看起来像

http://localhost/elibrary/public/

我想删除公共,我的网址应该是这样的

http://localhost/elibrary/

我的 .htaccess 文件包含。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /elibrary/

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?[^/])[?\s] [NC]
RewriteRule ^ /%1/ [L,R=301]

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

我读了这篇文章.htaccess: remove public from URL 并提出了上面的 .htaccess 文件。此 .htaccess 文件成功删除公用文件夹,但所有 Web 格式(如 css)都丢失了。 但是当我尝试这个http://localhost/elibrary/index.php/ 时,它显示了 404 页面,但所有格式都回来了。

如何从我的网址中成功删除 public 和 index.php ?我正在使用 zend 框架 2

【问题讨论】:

  • 我按照上述建议更改了 .haccess 文件。但它在浏览器中显示了文件和目录列表。如果我点击公用文件夹,它会打开网站。为什么上述解决方案对我不起作用。

标签: apache .htaccess mod-rewrite url-rewriting zend-framework2


【解决方案1】:

就我个人而言,我会坚持使用标准的 .htaccess 文件,即;

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] 

并在您的 httpd.con /apache2.conf 中进行更改 - 根据您拥有的服务器,在 DocumentRoot 和 Directory 中包含“公共”

例如;

DocumentRoot "/var/www/html/elibrary/public/"

<Directory "/var/www/html/elibrary/public">

这样你就不会破坏任何其他编码/路径

【讨论】:

    【解决方案2】:

    将您的最后一条规则更改为:

    # remove /public/ from URL
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !/public/ [NC]
    RewriteRule ^(.*?)/?$ public/$1 [L]
    

    并确保在您的 css、js、图像文件中使用绝对路径而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2018-06-30
      • 2015-10-11
      • 2020-02-14
      • 2014-07-13
      • 2017-01-03
      • 2019-08-31
      相关资源
      最近更新 更多