GregP
GregP

Reputation: 135

Redirect to public folder when using ZF2

I'm using ZF2 (Zend Framework 2, v2.2) and I would like that http://www.foo.com/path/to/app/ redirect to the public folder.

I've tried a lot of solution but not of them allow the url to point to /path/to/app/public/index.php, while only displaying /path/to/app/.

I would appreciate any solution as I cannot create a virtual host as described in app skeleton README file.

Any help very much appreciated.

Cheers, Greg.

Upvotes: 3

Views: 6047

Answers (3)

Gratus D.
Gratus D.

Reputation: 877

Wouldn't it be easier to just set up a Virtual Host?

DocumentRoot "actual/path/to/application"
ServerName appname

this would mean that when you type

http://yourdomain.com/appname it will resolve to http://root/actual/path/to/application

Maybe I am not understanding the problem properly - but i would use a VirtualHost instead of complex Mod_Rewrite rules.

Upvotes: 0

GregP
GregP

Reputation: 135

I figured out a solution: https://github.com/gpfister/ZendSkeletonApplication

In short, I added a .htaccess file at the root of the application:

RewriteEngine On

# If URL to the application is http://foo.com/path/to/ZendSkeletonApplication/
# the set the base to /path/to/ZendSkeletonApplication/
RewriteBase /path/to/ZendSkeletonApplication/

RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]

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

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ public/index.php [NC,L]

And then I changed module/Application/config/module.config.php.

It is now working fine, though http://foo.com/path/to/ZendSkeletonApplication/public will fail, which is fine as I don't plan to access the app with this URL.

Hope it helps.

Upvotes: 5

Andrew
Andrew

Reputation: 12809

If you are using apache with mod rewrite Stick this in your htaccess found above your public folder (/path/to/app)

RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Upvotes: 0

Related Questions