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.
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]
Related
I have a WordPress website (mywebsite.com) running on a shared hosting server. Alongside, I made a node.js app running in the backend on port 3000, which has been programmed to have different API endpoints.
I would like to have the endpoints to be in the same domain as the website in specific URLs.
The main endpoint, which has been declared in the node.js selector environment, works just fine. Let's call it mywebsite.com/myapp.
The other endpoints (which have been declared in the express routes) work correctly outside the website environment but are instead caught by apache/wordpress if I try to access them on the same domain even as sub-uri.
For example, if I try to access mywebsite.com/secondendpoint or mywebsite.com/myapp/thirdendpoint, the request gets caught by Wordpress which loads the 404 page.
Now, I understand that I have to instruct the apache server to redirect the requests to the above mentioned URL to the node.js app.
As long as I don't have access to the apache server, the only choice I have to tweak the .htaccess files.
When I created the node.js app, the virtual environment has been automatically set through a .htaccess file in its own subfolder in my website public folder, where my whole website is: /public_html/myapp . It uses Phusion Passenger to handle the virtual environment the app is running in.
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/user/apps/myapp"
PassengerBaseURI "/myapp"
PassengerNodejs "/home/user/nodevenv/apps/myapp/12/bin/node"
PassengerAppType node
PassengerStartupFile app.js
PassengerAppLogFile "/home/user/logs/myapp.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
The above code works just fine. I tried to add an Alias as suggested in some tutorial:
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
SetEnv Alias /secondendpoint /apps/secondendpoint
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
In the root of /public_html/ I have the following .htaccess file
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^/secondendpoint/(.*)?$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
No matter what I try, when I try to access mywebsite.com/secondendpoint, I always get the Wordpress 404 message.
What am I doing wrong?
I even tried to create a subfolder in /public_html/secondendpoint which actually does proxy the traffic, but I feel like this is not the right way to do it.
So, after a week of attempts, I found out that the syntax of the RewriteRule was indeed wrong for what I was trying to achieve.
RewriteRule ^/secondendpoint/(.*)?$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
The correct syntax is
RewriteRule ^secondendpoint(.*)$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
As shown in the comment of the .htaccess file "should only be modified via WordPress filters".
In fact is does automatically overwrites the rules I wrote every time it updates.
The right way to do it would be tweaking the internal WordPress WP_Rewrite
which I'm still trying to understand.
My best attempt has been adding to functions.php of my theme these lines:
function wpd_wtf_rewrite_rule() {
add_rewrite_rule(
'secondendpoint(.*)$',
'http://127.0.0.1:3000/secondendpoint$1',
'top'
);
}
add_action( 'init', 'wpd_wtf_rewrite_rule' );
These lines are being translated in
RewriteRule ^secondendpoint(.*)$ /http://127.0.0.1:3000/secondendpoint$1 [QSA,L]
and updated into the .htaccess file.
The slash in front of the target is an issue I don't know how to fix.
However, the generated .htaccess string does not proxy the requests resulting in errors from the API.
I have 3 php apps on one server. I cant modify any apache files. Haw can I set .htaccess to remove web/ folder from url?
192.168.45.54/app1/
192.168.45.54/app2/
192.168.45.54/app3/web - I want to change it to 192.168.45.54/app3/
On adress 192.168.45.54/app3/web/ everythink is working fine. But haw can I remove web/ from url?
I created .htaccess files:
RewriteEngine On
RewriteBase /app3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
But when I go to 192.168.45.54/app3/ in symfony log files I have fallowing error:
No route found for "GET /app3/"
I use Symfony in 3.4 version. I think that problem is in .htaccess located in /web directory... Haw to configure .htaccess corectly?
I can't create virtual host becouse I haven't access to the server...
You need to make the web/ directory the document root of your website. If you have direct access to the webserver’s virtual host configuration, change the document root to the following line and restart Apache:
DocumentRoot /path/to/symfony/web/
If you can’t modify the virtual host file directly, your hosting provider will usually give you the possibility to modify the document root through some kind of online tool.
Currently I am developing a webshop in PHP. For this I use the Zend Framework 2.
All of my routes exist of a locale i.e. "nl_NL" or "en_EN", followed by a controller and an action www.mydomain.com/nl_NL/profile/login. Look here for an example of a route used in my project. This one belongs to the Profile Module: http://pastebin.com/jmim47w8.
If the visitor has no account, and no Cookie is set, the locale variable will first be set to the variable retrieved from the following function:
locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE'])
If a cookie is set that value will be used.
On the other hand, if a user does have an account and is logged in, the variable used will be retrieved from the user's profile.
My entire script does work on a localhost. However, on the server it doesn't.
When loading the page nothing gets shown. I have checked the error logs and couldn't find anything. However, when i place /nl_NL/ or any other locale that meets the requirements of the "locale route" regex in the url manually, it does work.
My first thought was that the url_rewrite apache module was not installed or activated, but since other websites use this module as well, and they are on the same server this doesn't seem logical. Of course I did try to check wether it was enabled by running some scripts, but to no avail. Since our server is setup to not allow shell scripts to be ran, or to check wether apache modules are enabled using phpinfo() it's quite hard to make sure.
I have tried to verify that my .htaccess file is correct, but I'm pretty much a noob at .htaccess. This is my .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]
To see what my IndexController looks like look here: http://pastebin.com/AEgm3Jmk.
All in all, I would really like to know how to get a Zend FW 2 project to work on a hosting server while still using url rewrite or any other way that makes it possible to use locale variables.
If there is something missing from this post that is needed to help me further, I will be happy to post it.
I have solved the problem, everything is working fine right now.
I found the problem to be in my local.php config file. The problem was
an error within this file, so it was actually a write error ( corrupt file ).
#Alex thanks for the help though, appreciate it.
I have my .htaccess file working in localhost. But its not working if i upload it in server. It throws me 404 error.
I am using Parallel Plesk 11.0.9 and i can't find conf file for the same on that. If anyone has any idea how to fix it or any workaround for url rewriting would be great help.
Anyway here's the code in htaccess:
RewriteEngine on
RewriteRule ^store/living/Hutches-Armoires-Side-tables-Coffee-tables-Entertainment-centers? store.php?store=Living
RewriteRule ^store/dining/sideboards-buffets-chairs-benches-Dining-table$ store.php?store=Dining
RewriteRule ^store/working/Bookshelves-Study-tables$ store.php?store=Working
RewriteRule ^store/accessories/Boxes-Photo-Frames-Mirror-Frames-Block-Stamps-and-Book-stands$ store.php?store=Accessories
RewriteRule ^store/hallway/Console-tables-Armoires$ store.php?store=Hallway
RewriteRule ^store/sleeping/Bed-Frames-Night-Stands-Dressers-Mirror-framesBed-Linens-Canopies-Curtains$ store.php?store=Sleeping
You may need to wrap your rewrite rules with:
<IfModule mod_rewrite.c>
...
</IfModule>
...probably a good idea anyway.
Or you could try putting your rules into a vhost.conf file in the conf directory immediately below the location of your httpdocs directory. For example on a Centos machine the web root might be
/var/www/vhosts/domain.com/httpdocs
and you should have a:
/var/www/vhosts/domain.com/conf
directory, this will contain a set of pregenerated Apache config files that Plesk creates. If there isn't already create a vhost.conf and add your rules between a set of
<Directory /var/www/vhosts/domain.com/httpdocs/ >
... your rules ...
</Directory>
Once you've created the vhost.conf file you will need to tell plesk about it with
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain domain.com
If you still can't get it to work you can add a log for mod_rewrite, see this relevant SO answer for details
Is your Plesk running IIS and supporting PHP via FastCGI or ISAPI? If that is the case, check whether URL Rewrite is installed and follow this guide to translate htaccess (for apache) into web.config (for IIS)
rewrite script in .htaccess
RewriteEngine on
RewriteRule ^index/page/(.*)$ index.php?page=$1
It works in local machine. but in server it does not work. can any body help me?
Have you verified that the rewrite module is loaded on your server? If the server is running apache you should be able to verify this with
apache2ctl -t -D DUMP_MODULES
and looking for the rewrite_module in the response. (NB: You may need apachectl depending on the machine you are using.)
If it is not loaded you might need to change your server configuration. This is doable (again, depending on the version of apache in use) with a step like
a2enmod rewrite
or ensuring your httpd.conf file contains
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
or by ensuring there's a symbolic link between the rewrite.load file /etc/apache2/mods-enabled and /etc/apache2/mods-available.
Don't forget to restart apache afterwards.