how can remove '/public' from url using ht

2019-01-15 14:15发布

问题:

I'm using zend framework 2 on xampp. I created .htaccess file in the root('htdocs/zendtest/.htaccess') which has the following code,

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} $ [NC]
RewriteCond %{HTTP_HOST} !/zendtest
RewriteRule ^(.*)$ /zendtest/public/$1 [R=301,L]

my 'zentest/public/.htaccess' code is,

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' code:

<?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();

when I open 'localhost/zendtest' on browser then it takes me to 'localhost/zendtest/public'.I want to remove 'public' from the url by setting htaccess only(which will work both in localhost and online server),without using virtual host.how can I do that ?(theres no 'index.php' file in 'localhost/zendtest/' directory)

-thanks.

Edit:

Update:

directory:

I have the following .htaccess (according to Ravi Thapliyal's answer) in the 'zendtest/' folder (theres no 'zendtest/index.php' now in the 'zendtest' folder.just .htaccess. all other folder structures are same as the 'directory' picture.)

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /zendtest/

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

but it has now the following issue when open 'localhost/zendtest/' on the browser-

RESOLVED:

I had to make some changes like the following picture (the .htaccess was good):

and achieved the goal-

回答1:

Change htdocs/zendtest/.htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /zendtest/

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

I'm not sure what's zentest/public/.htaccess trying to do but the above should rewrite /zentest to /zentest/public transparently. Now, it's up to your rules in public/.htaccess and the index.php there.



回答2:

What I do is setting up myself a local Vhost, which is accessible with Firefox. To do so I configure the Vhost and manipulate my /etc/hosts file.

<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>

To set AllowOverride to All is not really necessary because I could copy the .htaccess content into that Apache vhost-config file but it better emulates the productive environment.