Symfony 404 errors - php

On my local machine, the symfony project I have created works perfectly.
I have installed symfony on my testing server and it is working fine. When I go to the URL in the browser I get the "Symfony Project Created" page. I ran the check_configuration.php and everything checks out.
The problem I am having is: I have created my database and uploaded the table data. I have uploaded the files from my local machine to the server with FTP. When I try to access the module I created, it throws a 404 Error:
Not Found
The requested URL /mymodule was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Here's some examples:
On my local machine using MAMP, I use the following URL that works fine:
http://localhost:8080/frontend_dev.php/mymodule
After I have uploaded everything, if I go to the testing server URL:
http://my.url.com:8090/
I get the "Project Success" page with the /sf images and styles.
BUT If I try
http://my.url.com:8090/mymodule
I get the 404 error as if the page does not exist, which makes me thik smfony does not know about the frontend module.
Any Help?

You'll need to do two things:
1) Ensure that mod_rewrite is enabled in Apache.
2) Double-check the .htaccess file in the document root to make sure it is pointing to your app.php. Something along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

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>

Related

Apache, how to handle unknown php file like standard URLs?

I am have a Symfony website where I have renamed the main entry point so "index.php" can't accessed nor being indexed by search motors. It works well. But, when I try to access this file I get a 404 file not found, this 404 page is handled by Apache and not by the application, it's the standard 404 page:
Not Found
The requested URL was not found on this server.
I'd like URLs like /index.php (*.php in fact) to be handled by the Symfony application to display the customised error page with the good layout. My vhost look like this, I tried to add a "directory index" directive but without success.
# configuration/vhosts/prod/mywebsite.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.mywebsite.com
DocumentRoot /var/www-protected/mywebsite.com/public
<Directory /var/www-protected/mywebsite.com/public>
AllowOverride All
Require all granted
FallbackResource /my-secret-entry-point.php
</Directory>
RedirectMatch 404 /\.git
ErrorLog /var/log/apache2/mywebsite.com_error.log
CustomLog /var/log/apache2/mywebsite.com_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mywebsite.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mywebsite.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
This can be done with mod_rewrite, replying with 301:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Default error documents can be used to dispatch the error-codes to PHP:
ErrorDocument 404 https://mywebsite.com/error/page/404
ErrorDocument 500 https://mywebsite.com/error/page/500
Then one still can send whatever header() one wants - or just show a page with the same headers.

symfony2 - remove /web from urls

I'm managing a website written in symfony 2.2, hosted on a web hosting service.
I'm pretty much a complete neophite in web development, so I'm probably making some stupid mistake.
Anyway, I want to be able to access the site without the /web part.
Example: 'domain.com/symfony_site/web/mypage' -> 'domain.com/symfony_site/mypage'.
Let me say from the start: I cannot manually configure apache on the server, I only have FTP access to it.
I think I'm left with .htaccess files solution.
I have tried each and everyone of the solutions I found on the internet (most of them from this site) and none worked.
And yes, mod_rewrite is enabled.
I feel like the simplest solution would be to add a .htaccess file in the root directory of the site (the one which also contains the /web folder), with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/$1 [QSA,L]
</IfModule>
And this does seem to redirect properly, but then I get a "The requested URL web/mypage was not found on this server." if I visit 'domain.com/symfony_site/mypage'.
The desired home page 'domain.com/symfony_site' intead gives a "You don't have permission to access /symfony_site/ on this server.", so it looks like this is not even redirecting properly.
First, you need to move the .htaccess from the web directory to the root directory (the DocumentRoot).
Then, open the moved .htaccess, search all occurrences of /app and replace them by /web/app.
The .htaccess (without comments) should looks like :
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/web/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /web/app.php/
</IfModule>
</IfModule>
Now your application can be correctly browsed using the project root directory as DocumentRoot.
This change may involve to adapt the way of loading your assets.
NOTE: It's a quick-and-dirty alternative assuming you cannot change the DocumentRoot of your application to make it points to the web directory.
a symfony2 app is designed to have the document-root at web/
here is an example configuration for apache vhost.
<VirtualHost *:80>
ServerName your.domain
DocumentRoot "/path/symfony/web"
<Directory "/path/symfony/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
so your rewrite rule is not necessary

Apache host many laravel projects in subdirectories

I have set up an Apache server with the following URL: https://dev.mysite.com. I am now creating subdirectories for each project that I have in development. For example it might be https://dev.mysite.com/project, or https://dev.mysite.com/anotherproject. Some of the projects in development utilize the Laravel, and the Laravel Lumen, framework(s).
I will not be using a vhost file for each project, as they will be existing in subdirectories. The goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, or Lumen, application served to the user visiting the project. I am ok with having the client visit /public/ to view their project. This is not happening.
-Indexes has been set on the server's virtualhost file to prevent listing files/directories in the browser.
When I navigate to {url}/project/public/heartbeat (for example) the route is not being served (or parsed) correctly. When I do a die of the $_SERVER['REQUEST_URI'] I get /project/public/heartbeat. The route heartbeat is set up as follows: $app->get('/heartbeat', 'Controller#method');. Visiting this url gives the typical HTTP not found exception from the Illuminate code. However; when I add project/public/ to the beginning of the route path (resulting in $app->get('/project/public/heartbeat'...) it works. I have tried adding the RewriteBase / and RewriteBase /project/public/ and RewriteBase /project/ settings to the .htaccess file and neither work. I do know that the .htaccess is being used (I can add 'asdf' to the file and break it). The mod_rewrite module is enabled.
Here are the configuration files for the vhost, and the project/public/.htaccess file.
.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
vhost:
<VirtualHost *:80>
ServerName dev.mysite.com
Redirect permanent / https://dev.mysite.com
</VirtualHost>
<VirtualHost *:443>
ServerName dev.mysite.com
DocumentRoot /var/www/public
<Directory /var/www/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/dev-error.log
CustomLog ${APACHE_LOG_DIR}/dev-access.log combined
ErrorDocument 403 https://dev.mysite.com
</VirtualHost>
Please let me know if you need any further information. Again, end goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, Lumen, etc. application served to the user visiting the project.
Setup your vhost as follows:
<VirtualHost *:80>
DocumentRoot "/some/directory/mysite.dev"
ServerName mysite.dev
</VirtualHost>
Notice you need to setup an initial 'root' project at /some/directory/mysite.dev. This doesn't have to be a laravel app, all it needs to have is a .htaccess file, and maybe you might want an index.html file in there so when you visit mysite.dev, you can at least see something. This is optional though.
In the /some/directory/mysite.dev folder, create a .htaccess file and add the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# We need this to stop the rewrite rule looping as the URI
# and directory both start with 'SubProject'
RewriteCond %{REQUEST_URI} !^/SubProject/
# direct all requests that start with SubProject
RewriteRule ^ SubProject/?(.*)/?$ /SubProject/public/index.php [L]
</IfModule>
When you visit mysite.dev/SubProject the index.php file from that project should be loaded.

Problems with codeigniter on ubuntu

I’ve got Codeigniter on Ubuntu 10 (LAMP). I have an htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dort
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php
#controller, previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /dort/index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends
#the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dort/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Up until now, we connected the remote server with an IP address,my base site is sitting under a folder called
‘dort’, so we used to call it like this: http://some_ip/dort
now we mapped a virtual host to dort, so we call it like: http://demo.dort.com/ and suddenly nothing
works except for the login page (index.php). I’ve changed $config[‘base_url’] in config.php but i
keep getting 404.
Please help :(
Did you use virtual host?
I have same problem and solve with this
1) Make virtual host in my case is like this: file /etc/apache2/sites-available/default
<VirtualHost *:80>
ServerAdmin admin#email.com
DocumentRoot /var/www/dort
ServerName yourdomain.biz #this domain must be define tld info.
ErrorLog /var/log/apache2/error_2.log
<Directory /var/www/dort>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
</VirtualHost>
2) Setting host in file /etc/hosts
127.0.0.1 localhost
127.0.0.1 yourdomain.biz
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
3) .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Send admin URL's to the admin controller (then skips the rest of the redirect rules)
RewriteCond %{REQUEST_URI} ^/admin(.*)
RewriteRule ^(.*)$ /index.php/admin/$1 [L]
# Redirects any request thats not a file or directory through to the main controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/main/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
4) Make sure when restart apache not show error
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
5) Go to browse and run this url http://yourdomain.biz
Above solution worked for me. I just want to highlight one thing. Make sure you have AllowOverride All instead AllowOverride None in virtual host file at /etc/apache2/sites-available/default

How do I map different server names to differrent DirectoryIndex in PHP Symfony?

Symfony uses the following typical .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
and I have this as my vhost:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
DocumentRoot "C:/wamp/www/jobeet/web"
DirectoryIndex index.php
<Directory "C:/wamp/www/jobeet/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf "C:/wamp/lib/symfony-1.4.1/data/web/sf"
<Directory "C:/wamp/lib/symfony-1.4.1/data/web/sf">
AllowOverride All
Allow from All
</Directory>
The above works perfectly, but I want to know how to map and additional debugging url, http://jobeet.dev to automatically serve the frontend_dev.php file so I can use urls like:
http://jobeet.dev/jobs/...
instead of
http://jobeet.dev/frontend_dev.php/jobs/...
to map to the debug .php file in the framework.
I tried adding a duplicate of the vhost entry and simply changing the servername and directoryindex to
ServerName jobeet.dev
DirectoryIndex frontend_dev.php
but understandably this does not work, as I believe I would need to check the URL in the .htaccess to do this?
Can anyone offer some advice regarding this?
Thanks in advance!
:)
First add jobeet.dev as a ServerAlias in your current VirtualHost so it can share the same hosting configuration:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
ServerAlias jobeet.dev
DocumentRoot "C:/wamp/www/jobeet/web"
....
Don't forget to restart Apache when you're done.
Next, turn on no_script_name in your dev configuration in apps/frontend/config/settings.yml:
dev:
.settings:
no_script_name: true
Now your dev web controller (frontend_dev.php) won't show up in your auto-generated URLs (from link_to(), url_for(), etc).
Finally, set up a RewriteRule for your dev domain before your production controller comes in to play to route everything coming in at jobeet.dev to your dev web controller:
RewriteEngine on
...
...
RewriteCond %{HOST_NAME} ^jobeet\.dev$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ frontend_dev.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
That should do it.
I'd recommend you to map all needed hosts (jobeet.loc, jobeet.dev, etc) to SF_DIR/web, set index.php as dir-index (as you did) and in that file just run particular app with a particular env depending on $_SERVER['HTTP_HOST'].
Hope I described good to make an idea clear.

Categories