score:1

Accepted answer

Problem is that Symhony's front controller /app.php also gets redirected to HTTPS since RewriteCond %{REQUEST_URI} !^/api/special/url/token still holds true. Replace your VirtualHost rules as this:

RewriteCond %{REQUEST_URI} !^/(api/special/url/token|app\.php)
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_URI} !^/(api/special/url/token|app\.php)
RewriteCond %{HTTP_HOST} ^www\..+$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Don't forget to restart your Apache server after making this change. Once that is done that unwanted redirection to HTTPS will be stopped.

score:2

I think the issue is that the first configuration is in the virtual host, the second one is in the .htaccess in the folder, htaccess has priorities, lets consider this structure

├──folder1
│  ├── .htaccess1
│  ├── folder2
│  │   ├── .htaccess2
│  │   ├── folder3
│  │   │   ├──.htaccess 3

The rules inside .htaccess1 will be overridden by .htaccess2, and both will be overridden by options inside .htaccess3, the last priority is for the options inside the virtualhost it self.

So I assume there would be only 2 options, either move your rewrites from the virtualhost level into the symfony .htaccess file, or try escaping them by a condition from the .htaccess and wait for them to bubble back up to the virtualhost to apply the rules there.

I think a combined .htaccess like this would do the trick

DirectoryIndex app.php

RewriteEngine On

# https redirections
RewriteCond %{REQUEST_URI} !^/api/special/url/token
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

RewriteCond %{REQUEST_URI} !^/api/special/url/token
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

# symfony2 rewriting
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/app.php [L]

I got a comment though, I don't really understand what you're trying to do, you keep redirecting back and forth between www and non-www ?


Related Query

More Query from same tag