Unable to customize Symfony .htaccess

Hi all,
I have a big problem with the Symfony .htaccess. This is my first time with mod_rewrite, so I’m sorry in case of weird errors.
I have to rewrite www.example.com/$site, when $site has a correct domain, on www.example.com/go?site=$site. I had successfully setup a local Apache Server on my laptop and created a VirtualHost, with these rules:


<IfModule mod_rewrite.c>
    Options FollowSymLinks 
    Options ExecCGI
    RewriteEngine On
    
    ### Rewrite /$site => /go?site=$site
    RewriteCond %{REQUEST_URI} [a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}/?$
    RewriteCond %{REQUEST_URI} !\\.(php|xml|html|yml|css|ico|gif|jpg|jpeg|png|js|txt|swf|pdf|doc|tgz|gz|zip|bzip)$ [NC]
    RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]
</IfModule>

If I put these rules on Symfony .htaccess it doesn’t works!
I have tried to change the .htaccess so many times but I have obtained:

  • or nothing;
  • or a redirect loop.

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
    RewriteEngine On

    ### Debug
    #RewriteCond %{QUERY_STRING} !vardump
    #RewriteRule ^/(.*) http://www.example.com/$1?vardump&thereq=%{THE_REQUEST}&reqhost=%{HTTP_HOST}&requesturi=%{REQUEST_URI} [R=301,QSA,L]

    ### Symfony default rules
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]

    ### Remove www
    #RewriteCond %{HTTP_HOST} ^www\\.(.*) [NC]
    #RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,NC,L]

    ### Rewrite /$site => /go?site=$site
    RewriteCond %{REQUEST_URI} [a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}/?$
    RewriteCond %{REQUEST_URI} !\\.(php|xml|html|yml|css|ico|gif|jpg|jpeg|png|js|txt|swf|pdf|doc|tgz|gz|zip|bzip)$ [NC]
    RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]
</IfModule>

I’m guessing you need to combine those rules, like /<sitename>/<controller>/<etc>/<etc> ?

Yes, you’re right. I have other <controller> also.
In one of the many changes that I had write to .htaccess file, I was able to see:

But it failed other site path, like:

At this time, I don’t know how to reproduce these steps. Damn :frowning:

I’m still not entirely sure what you want.

You have a domain, say example.com, and you want URLs like

example.com/mydomain.com (where mydomain.com can be any domain name without subdomain)

to show

example.com/go?domain=mydomain.com

right?

And then there is a symfony install. How that does that come in to play? Do you need to pass domains to that as well? If so, wouldn’t it be easier to set that up in the routing of symfony itself?

If symfony has nothing to do with the domain part I would suggest the ordering of the .htaccess is wrong because symfony will pick up the request before your “go” script has a chance to pick it up.

I would order your .htaccess as follows:


Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
    RewriteEngine On

    ### Debug
    #RewriteCond %{QUERY_STRING} !vardump
    #RewriteRule ^/(.*) http://www.example.com/$1?vardump&thereq=%{THE_REQUEST}&reqhost=%{HTTP_HOST}&requesturi=%{REQUEST_URI} [R=301,QSA,L]

    ### Remove www
    #RewriteCond %{HTTP_HOST} ^www\\.(.*) [NC]
    #RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,NC,L]

    ### Rewrite /$site => /go?site=$site
    RewriteCond %{REQUEST_URI} [a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}/?$
    RewriteCond %{REQUEST_URI} !\\.(php|xml|html|yml|css|ico|gif|jpg|jpeg|png|js|txt|swf|pdf|doc|tgz|gz|zip|bzip)$ [NC]
    RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]

    ### Symfony default rules
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
   
</IfModule>

Also

1. Get rid of the <IfModule …> and </IfModule>. Once you know the server supports mod_rewrite there is no need to ask Apache again and again for every request.

2. Ditch the line RewriteRule ^$ index.html [QSA] as that is something DirectoryIndex should manage, not mod_rewrite. If index.php is not the default, put DirectoryIndex index.php at the top of your .htaccess

3. Instead of


RewriteCond %{REQUEST_FILENAME} !-f

make it


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

to avoid Symfony hijacking requests to existing directories

4. The rule


RewriteRule ^([^.]+)$ $1.html [QSA]

is a bit silly. Change it to


RewriteCond %{REQUEST_FILENAME}\\.html -f
RewriteRule ^([^.]+)$ $1.html [QSA]

to make sure the .html file actually exists before you load it. No point in loading an alternative file if it doesn’t exist.

HTH :slight_smile:

Hi ScallioXTX,
thank you so much for the precious support.
I have understand useful stuffs about mod_rewrite with your reply, but my problem persists.

Our site works fine but the uri rewirte on example.com/go?site=www.google.com when we write example.com/www.google.com doesn’t works. Apache return us the custom “Page not found” of Symfony again.

My .htaccess now is:


Options +FollowSymLinks +Indexes +ExecCGI

RewriteEngine On

### Debug
#RewriteCond %{QUERY_STRING} !vardump
#RewriteRule ^/(.*) http://www.example.com/$1?vardump&thereq=%{THE_REQUEST}&reqhost=%{HTTP_HOST}&requesturi=%{REQUEST_URI} [R=301,QSA,L]

### Remove www
#RewriteCond %{HTTP_HOST} ^www\\.(.*) [NC]
#RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,NC,L]

### Rewrite /$site => /go?site=$site
RewriteCond %{REQUEST_URI} [a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}/?$
RewriteCond %{REQUEST_URI} !\\.(php|xml|html|yml|css|ico|gif|jpg|jpeg|png|js|txt|swf|pdf|doc|tgz|gz|zip|bzip)$ [NC]
RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]

### Symfony default rules
RewriteCond %{REQUEST_FILENAME}\\.html -f
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

I don’t know what should I do :frowning:

Indeed, example.com/www.google.com doesn’t work because it isn’t matched. The RewriteRule you have there doesn’t allow for subdomains (for all intends and purposes, “www” is a subdomain).

example.com/google.com should work though. Doesn’t it?

I’m sorry but example.com/google.com doesn’t work too.
This regular expression [a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}/?$ doesn’t match all the domain with or without the ‘www’?
Or I should rewrite it in this mode: ^/(www)?(\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}/?$?

Our rule should work with:

Try this


### Rewrite /$site => /go?site=$site
RewriteCond %{REQUEST_URI} ([a-zA-Z0-9\\-\\.]+\\.)?[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,4}/?$
RewriteCond %{REQUEST_URI} !\\.(php|xml|html|yml|css|ico|gif|jpg|jpeg|png|js|txt|swf|pdf|doc|tgz|gz|zip|bzip)$ [NC]
RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]

Nothing to do.
These rules works well on local Apache that I had setup on my notebook… without symfony. But with the framework installed (on test and production servers) these rules doesn’t works.

I think that the problem is on this section:


### Symfony rules
RewriteCond %{REQUEST_FILENAME}\\.html -f
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

And what if you change

RewriteRule ^/(.*)$ go?site=$1 [R=301,NC,L]

to

RewriteRule ^(.*)$ go?site=$1 [R=301,NC,L]

?

(ie. remove the / after the ^)

OMG! I see the rewrite works but not in the right mode.
The url now is: example.com/var/www/frontend/web/go?site=www.google.com

Put a slash in front of it

RewriteRule ^(.*)$ /go?site=$1 [R=301,NC,L]

:slight_smile:

If that doesn’t work, put the full domain in front of it:

RewriteRule ^(.*)$ [noparse]http://www.yourdomain.com[/noparse]/go?site=$1 [R=301,NC,L]

Thank you ScallioXTX. I add a / before go?site= and now it’s perfect!
Solution:

RewriteRule ^(.*)$ /go?site=$1 [R=301,NC,L]

Thanks!!!

Fantastic!! :weee:

:slight_smile: