如何从ZF2中删除public和index.php

时间:2014-03-19 16:20:12

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

我想从我的网址中删除公用文件夹和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文件成功删除公用文件夹,但所有网络格式化如css都丢失了。 但是当我尝试这个http://localhost/elibrary/index.php/时,它会显示404页面,但所有格式化都会回来。

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

2 个答案:

答案 0 :(得分: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中包含“public”

例如

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

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

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

答案 1 :(得分:0)

将您的上一条规则更改为:

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

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