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
Related
Here's my full case:
mvcframework directory inside /var/www/html/ dir.
mvcframework contains a public dir with index.php acting as a frontloader.
I am creating a .htaccess file inside public directory with following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
</IfModule>
I am accessing this through virtual host url mvcframe.local that is setup to direct at public folder:
<VirtualHost 127.0.0.3:80>
ServerName mvcframe.local
DocumentRoot /var/www/html/mvcframework/public
<Directory /var/www/html/mvcframework>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/www/html/mvcframework/mvc-error.log
CustomLog /var/www/html/mvcframework/mvc-access.log combined
</VirtualHost>
When I access http://mvcframe.local or http://mvcframe.local/ it outputs index.php content inside public folder index.php as it should.
When I access http://mvcframe.local/?posts/hello it outputs:
Requested URL = posts/hello
But when I access http://mvcframe.local/posts/hello removing ?, it gives:
Not Found
The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at mvcframe.local Port 80
I am trying to figure out the solution searching for 2 hours and still haven't got the solution for it.
Alright, so after messing around and reading .htaccess tutorials for hours, I finally managed to understand my mistake. First of all, here is the video which helped me understand my mistake.
Here's my solution:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [QSA,L]
</IfModule>
First, I am checking if mod_rewrite is enabled. Then providing some options. Then turning on the RewriteEngine which is neccessary for enabling rewriting rules. Then giving the Rewrite Conditions for [If no directory] and [If not file] for whatever query is being supplied, for which I rewrite the rule basically telling it that check for all string that doesn't end with extension ".", simply direct them all to index.php?$1.
Basically anything that has mvcframe.local/something_here/some_here/... will become automatically mvcframe.local/index.php?something_here/some_here
For my setup, index.php is the frontloader so everything must go through this file.
When I load pages that are not root, the page won't load without index.php before the route. The error I get:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at mydomain.com Port 80
To start with I have a virtual host file containing:
//also tried adding DirectoryIndex here before <directory>
<directory /var/www/mydomain.com>
DirectoryIndex index.php
AllowOverride ALL
</directory>
and a .htacces in my public with :
<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>
I have another domain with the same .htaccess and appache config on the same server and it works fine. Also I did restart apache. If I use phpinfo on my index.php/route page I see at Loaded Modules:
mod_rewrite mod_setenvif
When running the website localy with xampp everything works fine.
For hours i'm trying now but I can't fix it or find any error (logs are empty) or any solutions that I haven't tried yet.
Edit:
Im using Ubuntu Ubuntu 14.04 x64 on a digital ocean VPS. And I tried turning it on and off again (as suggested). PHP Version 5.5.9-1ubuntu4.9. I followed this tutorial to config everything (except the direcoty part). I changed the location of the apache log and a file error.log is created on the given directory but no errors are in it.
My currently appache config is : (i've triple checked the white parts where the domain name is).
When I run apache2ctl -t D DUMP_VHOSTS I get
this looks fine to me, also tried disabling the default config but it didnt help.
Note: i've replaced my real domain with mydomain.com in reality I use my real domain on these spots.
Thought how do I know for sure that the conf file im editing is the one being used by the domain?
This is the correct Alternative htaccess rule for Laravel 5 suggested to use when the default doesn't work:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The Options +FollowSymLinks I believe plays an important role because the DirectotyIndex acts like a symlink.
Also check /app/config/app.php to make sure you haven't set the Config('app.url') to contain the index.php.
If you have laravel installed in a sub-directory see this:
How can I remove "public/index.php" in the url generated laravel?
Have you tried turning it all off (shutdown), and then turning it on again?
If everything is as you say, it should work. I would try a restart to see if it changes anything.
If it doesn't, please update with what OS you are using.
I ended up deleting the complete conf file and copied it again from the one thats working. I took the default .htaccess from laravel and it worked. Unfortunately I still dont know what was wrong though. The current conf file looks like
<Directory /var/www/mydomain.com>
AllowOverride ALL
DirectoryIndex index.php
</Directory>
My .htaccess in /public
<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>
I had almost exactly the same problem today, which #dasper also helped me out on. I am using digital ocean Ubuntu 14.04 also, and I had to run the command a2enmod rewrite then reload apache2. It seemed that even though rewrite appeared to be on, it actually wasn't.
First, make sure mode_rewrite is enabled. I am sure you have probably done this but I want to make sure nothing is missed.
Next, see what version of apache you are running since the syntax has changed between 2.2 and 2.4. This should not be a big deal and highly doubt this is the culprit but could save you trouble in the future
Next, try putting the rewrite condition inside of the vhost information instead of the htaccess file. This can help a little with performance as well as give you a console warn/err when restarting apache where the htaccess errors could be squelched.
<directory /var/www/mydomain.com>
Options -MultiViews
AllowOverride ALL
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</directory>
Beyond this point I would be at a loss without some more logging or information from the server. You can also add CustomLog into the vhost and report all requests processed by the server.
The problem could be with your virtual host configuration and/or a misplaced .htaccess file.
Make sure your .htaccess file is in the public/ folder and that the DocumentRoot is set to that public/ folder.
And also, the < Directory> directive isn't controlling your virtual host but rather controls the folder itself and how it can be (or can't be) accessed on the server. To change the configuration of the virtual host do so inside the < VirtualHost> directive.
Here is an example of a virtual host configuration:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/var/www/mydomain.com/public"
DirectoryIndex index.php
</VirtualHost>
Here is the default Laravel Apache config:
<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>
and here is an alternative:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Try to rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.
Solution found here
I believe your directory path is wrong. You need /public on the end because Laravel treats that directory as the web root. Try this:
<directory /var/www/mydomain.com/public>
DirectoryIndex index.php
AllowOverride ALL
</directory>
But the better solution is to put the contents of the .htaccess file in your virtual host. Like this:
<VirtualHost *:80>
DocumentRoot "/var/www/mydomain.com/public"
ServerName mydomain.com
ServerAlias *.mydomain.com
<Directory "/var/www/mydomain.com/public">
# Ignore the .htaccess file in this directory
AllowOverride None
# Make pretty URLs
<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>
</Directory>
</VirtualHost>
Also, you should make sure your config file is loaded by running:
apache2ctl -t -D DUMP_VHOSTS
This will print a list of all the loaded vhosts. If yours isn't in that list then make sure it is enabled by running:
a2ensite [name_of_your_config_file]
Then restart apache with:
service apache2 reload
One more note: don't forget to edit /etc/hosts and add 127.0.0.2 yourdomain.com
I'm try to use my Symphony 2 project without the /web slug in URL. I've added the following to the .htaccess file in my root folder (localhost/subdir).
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^web/app_dev.php - [L]
RewriteRule ^web/app.php - [L]
# Fix the bundles folder
RewriteCond %{HTTP_HOST} ^localhost/subdir/$
RewriteRule ^bundles/(.*)$ /web/bundles/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^localhost/subdir/$
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /web/app.php [QSA,L]
RewriteRule ^(.*)$ web/app_dev.php [QSA,L]
</IfModule>
This script loads my page, but It do not get any /web/bundle/mybundle/javascript/, /images/, or /css/ files. These are giving me a 404 error. Because in the <head> tag it says /bundle/mybundle/javascript/ without the /web prefix. The same is on web-profiler, debug toolbar etc.
How can I fix my .htaccess to make this work?
the web/ folder is ment to be the root web directory
so you have a misconfiguration, tell your apache to have web as document root. in httpd.conf sth. like:
<VirtualHost *:80>
ServerName name.localhost.com
DocumentRoot "/Projects/name/web"
<Directory "/Projects/name/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
otherwise for example your parameters.yml with database pass and so on could be accessible.
the web/.htaccess conf for rewrite the app.php/app_dev.php in url could look ike :
# 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 app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app.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.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>
I have recently added a .htaccess file to my website root in order to achieve something I have already asked in this question:
Create blog post links similar to a folder structure
Now, the top answer was brilliant and it worked on localhost, however on my server it doesn't.
To be honest with you, I am not quite sure what these statements in the .htaccess mean exactly. I would be grateful if somebody could explain them line by line.
So this is what I have
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c> # If the module for rewrite is available
RewriteEngine On # turn it on
RewriteBase / # set / to the relative root path
RewriteRule ^index\.php$ - [L] # don't rewrite index.php and stop the rest of the rules
RewriteCond %{REQUEST_FILENAME} !-f # if file does not exist
RewriteCond %{REQUEST_FILENAME} !-d # and file is not a directory
RewriteRule . /index.php [L] # show the index page instead
</IfModule> # close the IfModule clause
I suspect if it doesn't work on your server, that the root directory isn't set properly for the domain or server configuration. You'll wanna look into where the files are placed and how the httpd.conf is configured to use those paths.
Also, for next time Apache has a wonderful documentation on this module that should cover everything I've just said, in more depth.
The actual problem was that the rewrite was not enabled on my virtualhost.
So I enabled it in my virtualhosts and everything works now.
<VirtualHost *>
ServerName mysite.com
DocumentRoot /var/www/html/mysite.com
<Directory /var/www/html/mysite.com>
Options FollowSymLinks
AllowOverride all
</Directory>
</VirtualHost>
Hello I am using the following htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
For doing the laravel routing.
When I surf to example localdevurl/public/users I get the following error ( 404 )
Not Found
The requested URL /Users/username/Sites/sitefolder/public/index.php/user was not found on this server.
But as u see it shows the index.php in the error. When I put index.php in my url it does work indeed. I have turned on everything in my apache config.
httpd.conf
Can anyone see what I am doing wrong here?
In your .htaccess file, you are passing the request after the index.php part, instead of allowing laravel routing system to process it, this is:
RewriteRule ^(.*)$ index.php/$1 [L]
should be as the original .htaccess
RewriteRule ^ index.php [L]
I had the same problem with the index.php part, this is how I solved: There is a problem when you have userdir module enabled, which it seems to be the case based on the URL of the error message.
Possible solutions:
Create a symlink in the webserver folder to the public laravel project folder of the user web directory. e.g.: # ln -s ~/public_html/mylaravel4/site/public/ /var/www/mycoolsite
Then you can access http://localhost/mycoolsite
Replace RewriteCond and RewriteRule lines with this: FallbackResource /index.php
Make sure you have AllowOverride set to All instead of None for you particular vhost, and also set Order allow, deny rather than Order deny, allow in httpd.conf.
AllowOverride all
Order allow, deny