问题
I had finished my first web application using Zend Framework 2 and I'm about to put it online. But may web host doesn't allow me to change my vhost configuration! The .htaccess file is allowed.
My .htaccess file in Public folder is like this:
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]
My Folder Directory Structure is like this:
-ProjectName
-config
-module
-vendor
- zendframework
-public
- css
- img
- JS
- index.php
Index.php File:
<?php
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
return false;
}
// Setup autoloading
require 'init_autoloader.php';
// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
So my question is: How to set up my ZF2 app with only .htaccess files?
Also I want to call default my "Front" Module Call While my site www.example.com run.
Your Answer will be appreciated. Thanks in advance.
回答1:
It seems that you need to enable the mod_rewrite module and restart Apache.
Zend Framework 2 requires that you have Apache's *mod_rewrite* module enabled. The *mod_rewrite* module is used to rewrite requested URLs based on some rules, redirecting site users to another URL.
In Debian or Ubuntu Linux
To enable Apache mod_rewrite
module, type the following commands:
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/rewrite.load ./rewrite.load
The command above creates a symbolic link in the mods-enabled
directory,
this way you enable modules in modern versions of Apache web server.
Finally, restart Apache web server to apply your changes.
I> A symbolic link in Linux is an analog of a shortcut in Windows.
In Fedora, CentOS or Red Hat Linux
In these Linux distributions, mod_rewrite
is enabled by default, so you don't need to do
anything.
Restarting Apache Web Server
After editing configuration files, you usually have to restart Apache HTTP Server to apply your changes. You do this with the following command (in Debian or Linux Ubuntu):
sudo service restart apache2
or the following (in Fedora, CentOS or Red Hat):
sudo service restart httpd
As a result, you should see output like below:
* Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified
domain name, using 127.0.0.1 for ServerName
... waiting apache2: Could not reliably determine the server's fully
qualified domain name, using 127.0.0.1 for ServerName [OK]
来源:https://stackoverflow.com/questions/20858591/make-zend-framework-2-work-without-virtual-host