2

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.

2
  • Have you tried to move the web/app.php file to an index.php file at the root of your site? (it will also require to change the paths in this file) Commented Mar 6, 2016 at 15:00
  • what if you visit domain.com/symfony_site/mypage/web ? Commented Mar 7, 2016 at 23:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work. I renamed the .htaccess in /web to _.hta (I imagine this is equivalent to deleting it), and made a file with your content in the root directory. I get 404 errors.
Is the 404 handled by Symfony or by your server ? I tried it and I can make it works, also I need to to know the exact configuration of your server in order to adapt my answer. If you can give me the full path of your application (the whole DocumentRoot)
1

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

2 Comments

From the question : "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.". The user cannot accesses server configuration.
you should dig into something more advanced than your "consumer-hosting"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.