How to remove “public” from url using routeing in

2019-02-15 19:55发布

问题:

An one issue in my zend, i write rule in .htaccess to remove "public" from url as following,

------------------------------------------------------------------------ 
RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain-name.com/$ [OR]<br/>
ReWriteCond %{REQUEST_URI} !public/<br/>
ReWriteRule ^(.*)$ public/$1 [L]<br/>
------------------------------------------------------------------------ 

but there is ROUTE method in zend, i have use it for multiple language to set language code in url LIKE www.domain-name.com/en/ , using zend_controller_router_route_chain,

before this method implemented, my url is www.domain-name.com but
when i use this method in my zend project, may be it overwrites .htaccess rule of removing "public" from URL or something happen using same and "public" is displaying in url like www.domain-name.com/public.

so IS THERE ANY METHOD OR TRICK TO REMOVE PUBLIC FROM URL USING ANY METHOD OF ROUTE IN ZEND FRAMEWORK ???

Thanks,

MRJethva.

回答1:

The following works for me using ZF2.2

Create index.php on ZF2 root directory and add the following content:

<?php 
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';

Create .htaccess on ZF2 root directory and add the following content:

SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule .* index.php

Finally, add this conditional statement in the top of your layout.phtml file:

<?php 
if (defined('RUNNING_FROM_ROOT')) {
  $this->plugin('basePath')->setBasePath($this->basePath().'/public');
} ?>

Enjoy!

Reference: http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/



回答2:

Yes, and you don't need Zend_Route. Delete the public folder and put the Zend Framework files from it (index.php, .htaccess, etc) in your root directory (e.g. htdocs). You can place the application folder and other Zend Framework files outside of your web root where they cannot be accessed over HTTP.

All you need to do is edit index.php and change the APPLICATION_PATH to the correct path. This way your Zend Application will run from your root directory and you won't need to use mod_rewrite to hide the public folder.

See the last part of this answer for a similar example.



回答3:

zf2 and zf3 remove Public from URL

create index.php and .htaccess add file in root of project

add the line in index.php include_once("public/index.php");

and in .htaccess file

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] 

after that go to following path

Module->application(module name)->view->layout->layout.phtml change css and js path add public in path

before

->prependStylesheet($this->basePath('css/bootstrap.min.css'))

->prependFile($this->basePath('js/bootstrap.min.js'))

after

->prependStylesheet($this->basePath('public/css/bootstrap.min.css'))

->prependFile($this->basePath('public/js/bootstrap.min.js'))

also in image path