2014-03-19 32 views
0

我想從我的網址中刪除公用文件夾和index.php。如何從ZF2中刪除public和index.php

例 我當前的URL看起來像

http://localhost/elibrary/public/ 

我想刪除公共和我的URL應該看起來像

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頁,但所有格式化回來。

我怎樣才能成功地從我的網址中刪除公共和index.php?我使用Zend框架2

+0

我改變.haccess文件按照上述建議。但它顯示我在瀏覽器中的文件和目錄列表。如果我點擊公共文件夾,它會打開網站。爲什麼上述解決方案不適合我。 –

+0

檢查這篇文章http://stackoverflow.com/questions/9834328/how-to-remove-public-from-url-using-routeing-in-zend-framework –

回答

0

您的最後一個規則改成這樣:

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

並確保在你的CSS使用絕對路徑,JS,圖片文件,而不是相對的。這意味着您必須確保這些文件的路徑始於http://或斜槓/

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和目錄

如「公共」;

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

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

這樣你不會打破任何其他編碼/路徑