How to remove "web/app_dev.php" from Symfony2 URLs? - php

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

Related

Symfony (3.4) app : cant redirect requests to /web dir via .htaccess

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...

Symfony 2 removing app.php

i have been through lots of topic about this, and the thing is still not working so i was wondering if you could help me to see what i am doing wrong.
My goal is to have neat URL as www.name.com without the app.php nor web, and i have changed over and over my Virtualhost & Htacces that my page are kind of gone for now^^ (Yeah so pro)
So first, i have enabled the rewrite mod, and i am running a 2.2.2 apache version.
Here is my VirtualHost files :
<VirtualHost *:80>
ServerName steeph
ServerAlias name.com
DocumentRoot /home/ksc/www/symfony/web
<Directory /home/ksc/www/symfony/web>
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
I have also tried with line on :
<Directory "path">
DirectoryIndex app.php
Options -Indexes
AllowOverride All
Allow from All
</Directory>
which are not working either
For my htaccess i have tried at the symfony files root (and removing the /web from the virtual host) which was a fail, and i tried with this htaccess
<IfModule mod_rewrite.c>
RedirectMatch permanent ^/app\.php/(.*) /$1
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I tried with the "RedirectMatch permanent ^/app.php/(.*) /$1" and without
So i have been to like a dozen of stackoverflows and i have tried even not popular answer but it still not working, so i would really appreciate some help here
Thanks a lot!
If Linux... enable Rewrite
sudo a2enmod rewrite
sudo service apache2 restart
You could just copy the one from symfony standard edition
-> https://github.com/symfony/symfony-standard/blob/master/web/.htaccess
and it would just work fine.

mod_rewrite throws 404, however works on (almost) identical server

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.

running laravel on a different port

I'm using Bitnami MampStack on OS X 10.9. Because this is an inherited laptop, I had to set up Apache under MAMP to listen on port 8888. I tweaked it to listen on 8889 as well and added the following as a VirtualHost in my httpd.conf file:
<VirtualHost *:8889>
DocumentRoot "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public"
ServerName localhost
<Directory "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public">
Require all granted
</Directory>
</VirtualHost>
I followed the example here (so all my source code matches his). The index blade works, but the edit, create, and delete blades return a 404.
As I was testing, I discovered that http://localhost:8889/create returned a 404, but http://localhost:8889/index.php/create returned the correct view. Also, browsing http://localhost:8888/codebright/public/create works as expected.
So...me being kinda new to Laravel and MVC frameworks in general, is there some way I can have this installing running properly on port 8889?
Your virtualhost is working as it should, you just have now to be sure your public/.htaccess file is in place:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is the file that rewrites your urls removing the /index.php from them.
EDIT
Also, check if you have mod_rewrite installed and enabled, because your .htaccess uses it, in Ubuntu/Debian, to enable it, you have to execute:
sudo a2enmod rewrite
Figured it out, thanks to this. I was missing AllowOverride All in the <Directory> section. Updated httpd.conf:
<VirtualHost *:8889>
DocumentRoot "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public"
ServerName localhost
<Directory "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I thought I might have been missing RewriteEngine On in the VirtualHost definition but it doesn't seem to have an effect on my problem.

Unable to access routes without index.php in Laravel 4

Ok, so I have spent a considerable amount of time googling for suggestions for this, and despite setting up several test project in Laravel 4 before on WAMPserver, I am not sure what I am missing in this case. Hopefully I have provided all details necessary on my setup and what I have tried so far.
WAMPserver version 2.4, located in C:/wamp/
Apache version 2.4.4
PHP version 5.4.12
mod_rewrite enabled (I can see it in phpinfo())
Project located in C:/wamp/www/wsrp/
in C:/wamp/www/wsrp/public I have the following .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In my C:/wamp/bin/apache/Apache2.4.4/conf/httpd.conf file I have uncommented this line:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
In my C:/wamp/bin/apache/Apache2.4.4/conf/extra/httpd-vhosts.conf file I have the following:
<VirtualHost *:80>
DocumentRoot "C:\wamp\www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/wsrp/public"
ServerName localhost
Options Indexes FollowSymLinks
<Directory "C:/wamp/www/wsrp/public">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
In my C:/wamp/www/wsrp/app/routes.php file I have this test route:
Route::get('test', function() {
echo 'Hello world!';
});
To test I am trying to visit this URL:
http://localhost/wsrp/test
And this doesn't work, it shows me an Internal Server Error page. If however I visit this URL:
http://localhost/wsrp/index.php/test
I see my 'Hello world!' message displayed.
Obviously I want to be able to work on this project without having the index.php in the URL, that is the problem.
Of all the SO questions/answers I have looked at, they have all suggested adding a virtual host, which I've done, having the right .htaccess file in C:/wamp/www/wsrp/public/ which I've done, having mod_rewrite enabled, which I've done and yeah.. not sure what else to try, or what I am missing. Help and suggestions greatly appreciated!
Edit:
Forgot to mention that I also have an alias file in C:/wamp/alias/wsrp.conf
Alias /wsrp "c:/wamp/www/wsrp/public"
<Directory "c:/wamp/www/wsrp/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Allow from all
</Directory>
Firstly why do you have a localhost v-host? The root directory for WAMP servers is typically c:/wamp/www that is already mapped by HOSTS to 127.0.0.1 or localhost. That v-host is irrelevant.
Secondly, as your c:/wamp/www directory is being used to contain your sites, httpd.conf already contains the <Directory> details it needs, from memory it looks something like:
<Directory c:/wamp/www>
Options Indexes FollowSymLinks
AllowOverride All
Require local
</Directory>
You don't need to define any special directory information for sub directories unless you want to add specific module configuration or change any of the defaults. To get your vhost working you need add no more than:
<VirtualHost *:80>
ServerName mylocalsite.dev
DocumentRoot c:/wamp/www/mysite/public
</VirtualHost>
I would suggest however that you put in locations for error logging / access logging.
I've had the same problem that you. I solved this problem in this way.
Open C:\wamp\bin\apache\Apache2.4.4\conf/httpd.conf
Then, You may search the next line, #LoadModule rewrite_module modules/mod_rewrite.so
Remove the pound ('#') sign at the start and save the file.
Next, restart your WampServer and test.
I hope help you!

Categories