I recently deployed a symfony project and noticed that the only way I can currently access the project is using the following URL:
http://domain.com/app.php/index
I was wondering if there is a way to automatically get the app.php to be served without actually including it in the URL.
Something like:
http://domain.com/index
Not sure if htaccess is required for this or if this is something that can be accomplished directly using Symfony.
This is what my htaccess currently looks like:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php/ [QSA,L]
</IfModule>
I appreciate any advice, thanks in advance!
This is (effectively) the .htaccess file which I use on several projects and sites using Symfony 2:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I also have the following vhost entry for one of my Symfony projects under the apache server:
<VirtualHost *>
ServerAdmin admin#localhost
DocumentRoot "<PATH_TO_PROJECT>/web"
ServerName localhost
ServerAlias localhost
ErrorLog "<PATH_TO_PROJECT>/app/logs/apache_error.log"
ErrorLogFormat "[%t] [%l] [pid %P] thread-%T %F: %E: [client %a] %M"
<Directory "<PATH_TO_PROJECT>/web">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
The omission of the DirectoryIndex app.php index may be enough in the .htaccess file, so you could try that. Admittedly my knowledge of Apache configuration is weaker than I would like it to be, hopefully this helps, if not--hopefully some one out there can give you a better, stronger answer?
Best of luck!
Related
I've a website lets say website.fr ( on server : /web/website ) ;
I ve added a symfony blog app in /web/website/blog/.
I dont manage to access the blog via website.fr/blog/ (404 error)
There is a .htaccess file in /web/website/blog/ directory, and one in /web/website/blog/web directory.
I am used to deploy symfony apps 'alone' on a server, and make the domain point to the web dir of the app, and everything always works fine, but in this configuration, I cant get it to work.
I have tried to use the following .htaccess conf under /web/website/blog/ :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And I did not edit the default one under /web/website/blog/web/
Im not sure what Im doing wrong, so any help would be appreciated !
For me I use .. nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
which is explained Here and restart Apache, but I do not know if I understand your problem
I couldn't get what I wanted, but here is the solution I implemented :
I created a .htaccess in /web/website dir, that rewrites all requests to /web/website/blog, with the root (/) excluded from this rewriting :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ blog/web/$1 [QSA,L]
</IfModule>
That's not the way I wanted to get my rewriting, but I have the result I expected ; note that it would not be a good solution for a website that would evolve with new modules, routes, folders...
I'm working on a new website which is supposed to replace an old one. The new website will use Phalcon and it's using its own routing system. The new website will be accessible using www.mydomain.com, like the previous one. The new website DocumentRoot is /var/www/mydomain-www, the old one is instead /var/www/mydomain-platform.
Inside this "platform" there are some "web services", that many other applications (included an iOS and an Android apps) are using. Unfortunately, the new website must use these web services as well. :-(
Basically there is a PHP file for each API call and all these files are located in the root of the old website, specifically /var/www/mydomain-platform. I know it's a mess but I have to deal with that, I have inherited it. In the old website root there is also a .htaccess file that contains a rewrite route for every single API call, to the right PHP file. They look like:
RewriteRule ^ws/event/games(.*)$ /ws_events.php$1
RewriteRule ^ws/favourites/list(.*)$ /ws_teams_favorites.php$1
I need to find a way to rewrite all the url for www.mydomain.com/ws/(.*) to the directory /var/www/mydomain-platform/ws, because the web services' files are there and I don't really want to move them in the root directory of the new website.
I thought to create a symlink var/www/mydomain.www/ws pointing to the directory /var/www/mydomain-platform/ws, but I'm sure the below vhost configuration will not like it, because it will redirect everything to /var/www/mydomain/public.
I would like to put these redirect stuff inside a .htaccess, and leave the vhost like it is.
Any idea?
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAdmin dev#mydomain.co.uk
DocumentRoot "/var/www/mydomain-www"
ErrorLog "${APACHE_LOG_DIR}/www.mydomain.com-error_log"
CustomLog "${APACHE_LOG_DIR}/www.mydomain.com-access_log" common
AllowEncodedSlashes On
AddDefaultCharset UTF-8
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [B,NE,L]
</IfModule>
</Directory>
<Directory /var/www/mydomain-www/public>
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,NE,QSA,L]
</IfModule>
</Directory>
<FilesMatch "(?i)\.php$">
Require all denied
</FilesMatch>
<FilesMatch "index\.php$">
Require all granted
</FilesMatch>
</VirtualHost>
You can move ws folder to /var/www/mydomain-www directory or create a symbolic link in the new directory. Then you need to change rule in /var/www/mydomain-www to skip anything with /ws/ in front:
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ^/?((?!ws2/|ws3/).*)$ public/$1 [B,NE,L,NC]
</IfModule>
</Directory>
I have a subdirectory called test, http://www.mywebsite.com/test/. Inside of the directory is an index.php file.
I want to put an .htaccess file inside of the test subdirectory that will rewrite the URLs so that http://www.mywesbite.com/test/paul will become http://www.mywebsite.com/test?author=paul, and so that urls like http://www.mywebsite.ocom/test/sam/3 will become http://www.mywebsite.com/test?author=sam&test=3
This is what I have so far:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/(.*)/?$ /test/index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} (/test/)
RewriteRule ^(.*)/(.*)/?$ /test/index.php?author=$1 [L]
Unfortunately, I get a 404 Not Found error. Am I missing something? Any thoughts are much appreciated!
EDIT: I checked phpinfo() and mod_rewrite does seem to be showing as a loaded module.
This is what my virtual host file looks like too. It looks like AllowOverride is set as well.
<VirtualHost *:80>
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot /var/www/html/mywebsite.com/httpdocs
ErrorLog /var/log/apache2/mywebsite.com-error_log
CustomLog /var/log/apache2/mywebsite.com-access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
ScriptAlias /cgi-bin/ "/var/www/html/mywebsite.com/httpdocs/"
<Directory "/var/www/html/mywebsite.com/httpdocs/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<IfModule mod_userdir.c>
UserDir public_html
Include /etc/apache2/mod_userdir.conf
</IfModule>
</VirtualHost>
EDIT 2: I also wanted to mention, I put some junk text in the .htaccess file to try and break it, and instead of a 404 error I got an internal server error, so it does seem like the .htaccess file is being read.
I tested Panama Jack's solution and it seems like the RewriteRules are not accounting for this already being in /test/.htaccess... so this would not work for the first example (/test/paul). Proposing an alternate solution that should work for both examples:
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.+)/(.+)/?$ index.php?author=$1&page=$2 [L]
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^(.+)/?$ index.php?author=$1 [L]
I have a broken Cake PHP site that I did not write, but am charged with fixing. I have the main controller up, however no css/js/images, and when I click a link I get a 404 not found. I believe something is incorrect with mod_rewrite or the docroot config in apache.
While reading through Cake documentation, I came across this:
"app/webroot
In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files."
In my /etc/apache2/sites-enabled/this-site, I see this:
DocumentRoot /u02/data/docroots/this_site
<Directory /u02/data/docroots/this_site>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
</Directory>
So, my question:
does the above config block need to have:
/u02/data/docroots/this_site/app/webroot as the DocumentRoot and ?
Anywhere else you can think to look for troubleshooting this?
Since you have access to edit the apache config files - it's most appropriate to make a standard production install.
Fixing the docroot
does the above config block need to have: .../app/webroot as the DocumentRoot
Assuming that path exists: yes.
This will fix the broken css/js/images.
Fixing mod rewrite
Put the rewrite rules in the virtual host config:
DocumentRoot /u02/data/docroots/this_site/app/webroot
<Directory /u02/data/docroots/this_site/app/webroot>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
// added
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
This will then route requests for anything that's not a file to the application.
Verify that the rewrite rule used is compatible with the version of CakePHP in use, the above is based on the rule for the latest release - it changed over the years and using the wrong rewrite rule may have unexpected side effects.
What's actually broken
AllowOverride none
This prevents the .htaccess files from being read by apache. If you were to change this to AllowOverride all, and assuming the currently-ignored .htaccess files exist, the site would work - but as a development install.
Try this:
<VirtualHost *:80>
ServerAdmin xxxxxx#test.ro
ServerName test.ro
ServerAlias www.test.ro
DirectoryIndex index.php
DocumentRoot /u02/data/docroots/this_site
ErrorLog /u02/data/docroots/this_site/error.log
CustomLog /u02/data/docroots/this_site/access.log combined
<Directory "/u02/data/docroots/this_site">
Options -Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And you should have 3 .htaccess files:
/u02/data/docroots/this_site/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
/u02/data/docroots/this_site/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/u02/data/docroots/this_site/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Hope this helps ;)
i recently fixed my site up with a bit up php mvc magic for which i use those settings in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
it worked like a charm. the only problem i encountered was that when i reference files like css or images relatively, the browser assumed that we moved down the folder structure due to the dashes in the url
localhost/projectname/controllername/action/id
so the css/styles.css was requested at localhost/controllername/action/id/css/styles.css
having absolute paths fixed that problem but didnĀ“t work well with having a localtest server and a remote live and development server after there was no "projectname"- folder
i decided to configure my local wamp server so i can access my different project as a subdomain to my localhost like "http://projectname.localhost".
i activated the relevant apache modules, edited my hosts file and changed my httpd-vhosts.conf to
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost.com
ServerAlias www.localhost.com
DocumentRoot "C:\wamp\www"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost.com
ServerAlias *.localhost.com
VirtualDocumentRoot "C:\wamp\www\subdomains\%1"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www\subdomains\%1">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
now when i call projectname.localhost.com/controllername/action/id it tells me
The requested URL /subdomains/projectname/index.php was not found on this server.
while projectname.localhost.com/index.php still supplies me with the right file.
i have heard that there might be a problem with the $_SERVER['DOCUMENT_ROOT'];. that doesn't get set properly but knowing that has not helped me fix that problem yet.
can someone point me into the right direction or suggest another environment configuration that makes testing under those circumstances possible?
changing my .htaccess to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
solved that issue.
the magic line is RewriteBase /
seems like the server was really looking for the index.php in the wrong place.
hope this helps other people having the same problem... :)