I have two VPS's of mine serve the same PHP application, however whitelabled to two different client's brands. The underlaying code is identical. I use a .htaccess to redirect all requests to index.php, and then use my MVC to render pages etc. This is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
It is the same on both servers. My Apache VirtualHost looks as follows:
<VirtualHost *:80>
ServerAdmin benedict#ovalbit.com
ServerName [REDACTED]
ServerAlias [REDACTED]
DocumentRoot /var/www/cloud/
DirectoryIndex index.php
LogLevel Error
ErrorLog /var/www/cloud/logs/error.log
CustomLog /var/www/cloud/logs/other.log Combined
<Directory /var/www/cloud/>
Options +ExecCGI
AllowOverride all
</Directory>
</VirtualHost>
Running a2enmod rewrite returns Module rewrite already enabled.
I am almost 100% positive that the file it is redirecting to it available, is there any way to log the result of mod_rewrite requests so I can be sure? Can anyone see any stupid errors I might have made?
I'm certain it's not due to the Apache config as I have another site on the same VPS which also uses a mod_rewrite and works fine.
Related
I know this question is asked a lot but I read all the questions related to this but they didn't solve my problem.
I just deployed a Symfony 3 web app and I followed the Symfony server configuration documentation just to find out that the www.domain.com displays the directory structure!
Here's the configuration that I used of file /etc/apache2/sites-available/site.com.conf:
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
I can see the app when I go to www.domain.com/web and www.domain.com/web/app.php
I want to be able to visit www.domain.com and see the app.
Change the line:
AllowOverride None
to next one:
AllowOverride All
Remove section <IfModule mod_rewrite.c> And use standard Symfony's .htaccess file.
Also you need to set up PHP as module for your apache. Be sure to have uncommented line in http.conf that loads PHP module:
LoadModule php7_module libexec/apache2/libphp7.so
You are missing your DirectoryIndex app.php or DirectoryIndex dev_app.php contained in your directory tag. This will cause apache to load app.php if a file has not been requested.
https://httpd.apache.org/docs/2.0/mod/mod_dir.html
<Directory /var/www/project/web>
DirectoryIndex app.php
...
</Directory>
Also make sure mod rewrite is enabled.
a2enmod rewrite
You may have to use sudo.
One final note:
Options -MultiViews should be contained inside the <Directory> for use all the time and not just when mod_rewrite is enabled.
My .htaccess file code is
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
My domain link https://www.spectrumdialogues.com/
But when I hit link it is showing homepage properly, but in every page inside menu it is showing Not Found error. Because all menu URL's generated dynamically using Symfony2 route.
What is the .htaccess configuration if URL's generated dynamically like news website article's URL's?
Please guys help me!!!!
Try to change you Apache config file:
<VirtualHost *:80>
ServerAdmin admin#yoursite.com
ServerName yoursitename.com
ServerAlias www.yoursitename.com
DocumentRoot /home/admin/projects/yoursite
<Directory "/home/admin/projects/yoursite/web/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yousite_error.log
CustomLog ${APACHE_LOG_DIR}/yousite_access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
If this is not work, you have to see you Apache2 error file. For example /var/log/apache2/yousite_error.log
In your virtual host setting
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/project
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/certificate.pem
SSLCertificateKeyFile /etc/apache2/ssl/privatekey.pem
<Directory /var/www/html/project>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
The error is due the permission issue for your project directory. Adding part resolved the issue in my case.
I recently set out to compile a lamp stack from source. Apache was compiled like so:
./configure --prefix=/srv/www --enable-mods-shared=most
and I can confirm mod_rewrite is enabled by placing this in the vhost for a site:
Redirect "/foo.html" "/bar.html"
However, after installing wordpress, I can log in at example.com/wp-login.php, but example.com/wp-admin loads to an error page about a redirect loop.
I'm using the default wordpress htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
and this vhost:
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/srv/www/htdocs/example.com"
<Directory "/srv/www/htdocs/example.com">
options indexes followSymLinks
# AllowOverride controls what directives may be placed in .htaccess files.
AllowOverride All
# Controls who can get stuff from this server file
require all granted
rewriteEngine on
</Directory>
<IfModule mpm_peruser_module>
ServerEnvironment apache apache
</IfModule>
ErrorLog /srv/www/logs/example.com/error_log
CustomLog /srv/www/logs/example.com/access_log combined
</VirtualHost>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/www/htdocs/example.com/$1
DirectoryIndex /index.php index.php
The ProxyPassMatch line is included to handle php via php-fpm.
I can include the httpd.conf if needed, but it's 506 lines long and I hear pastebin isn't received warmly here. I've checked the error log for that domain and it doesn't get updated when hitting the redirect loop, and it seems like a pretty straightforward setup with the exception of using php-fpm instead of mod_rewrite, so I'm a bit lost on where to check further.
This was compiled on centos7 so a2enmod isn't an option.
DirectoryIndex /index.php index.php
This was the issue.
DirectoryIndex index.php
This was the solution. After removing /index.php from the directoryindex directive, the site loads and the dashboard loads without issue as well.
Has a fresh setup of Apache 2.2 + PHP 5.3. Trying to run Yii site, which requires .htaccess file to be as following:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
But instead of running index.php file it lists the directory.
What I know:
.htaccess is being read and processed - if you break it, you get 500 IE.
mod_rewrite is On: LoadModule rewrite_module modules/mod_rewrite.so
PHP is up, I was able to run Adminer w/o any issues
Virtual Hosts file looks fine (see below)
httpd-vhosts.conf:
<VirtualHost *:80>
DocumentRoot "d:/denis/www/yii.local"
ServerName yii.local
ErrorLog "logs/yii.local-error.log"
CustomLog "logs/yii.local-access.log" common
# Other directives here
# DirectoryIndex index.php
<Directory "D:/denis/www/yii.local">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#AccessFileName htaccess
</VirtualHost>
Update: If I add Options -Indexes to .htaccess I am getting 403 Forbidden, due to "Directory index forbidden by Options directive".
I just created my first Symfony2 project. But the "/web/app_dev.php" part in the URL annoys me. It should be possible to do this without Virtual hosts...
But when I try this through .htaccess I always get routing errors and the "web/" is always added to the url...
EDIT: The whole project is also in a subdirectory named "symfonyTest". The Url to the demo page is "http://localhost/symfonyTest/web/app_dev.php/demo/" and it should become "http://localhost/symfonyTest/demo/". Links should also have this syntax. Is this possible?
Symfony2 comes with a built in debug mode, which is what you are using when you access url's with the app_dev.php addition. The debug mode caches significantly less than the production mode which can be accessed by directing your browser to the url, but leaving out the app_dev.php. If accessing this url doesn't work then it probably means that you need to clear your production cache.
To clear the cache direct you terminal (command console) to the root of you Symfony project. From there type the following command:
php app/console cache:clear --env=prod --no-debug
Per comments below in Symfony 3 this has moved:
php bin/console cache:clear --env=prod --no-debug
This should clear your productions cache and allow you to access urls without the app_dev.php.
As for the web part of the url. The easiest way to remove that is to set the web root of your project to the web folder. It's best to do this with apache using virtual hosts, but there are ways to do it with the htaccess.
This link explains how to use the htaccess file to change the webroot.
http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/
Hope this helps!
<VirtualHost *:80>
DocumentRoot "path_to_symfonyTest/web"
ServerName "symfonyTest"
<Directory "path_to_symfonyTest/web">
DirectoryIndex app_dev.php
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
RedirectMatch permanent ^/app_dev\.php/(.*) /$1
</Directory>
</VirtualHost>
That's part of my httpd.conf
I used to have this problem too, please take a look at my configurations and try them out
inside my .htaccess:
Hi all, I have been having this problem too. And it seems that the "stock" .htaccess is the problem. I have solved this in my environment and here is my .htaccess with notes:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app_dev.php [L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the startpage to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
inside my "symfony" entry in /etc/apache2/sites-available:
<VirtualHost 127.0.1.15>
ServerAdmin webmaster#localhost
ServerName symfony
DocumentRoot /var/www/Symfony/web
DirectoryIndex app.php
<Directory /var/www/Symfony/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
If you use an apache virtual host, you can get it working the way you desire. Here is an example virtual host from my xampp:
<VirtualHost *:80>
ServerName myurl.local
# Basic stuff
DocumentRoot "C:/path/to/symfony/web"
DirectoryIndex app.php
<Directory "C:/path/to/symfony/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
After restarting your apache, you can access the project through http://myurl.local (don't forget to configure your hosts file under C:\Windows\system32\drivers\etc\hosts!). If you want to have the dev environment (app_dev.php) not showing up in the url, change the DirectoryIndex to app_dev.php.
//enable mod rewrite
sudo a2enmod rewrite
Change all occurrences of AllowOverride None by AllowOverride All File: (/etc/apache2/apache2.conf) or (/etc/apache2/sites-available/000-default.conf) - for me work with /etc/apache2/apache2.conf file.
Example:
<Directory /var/www/web/>
# enable the .htaccess rewrites
Options Indexes FollowSymLinks
#AllowOverride None #commented
AllowOverride All
Require all granted
</Directory>
//Restart apache service: sudo service apache2 restart
You would need .htaccess file in parent folder of web, however, I wouldn't suggest doing so. That folder, apart from web, contains complete source, binaries, vendors and much more therefore you would need to apply many rules just to allow web-access to web folder and disallow web-access to everything else.
So, it's doable, but I would definitely go for vhost solution...
you just need to copy the content of .htaccess file to the your-website.conf
inside the TAG Directory