Symfony application shows directory listing when using https - symfony

Normal VHOST:
<VirtualHost *:80>
DocumentRoot /var/www/html/app/current/web
ServerAdmin me#app.foobar
ServerName app.foobar
ServerAlias www.app.foobar
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options Indexes FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !HTTP/1.1$
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<IfModule mod_headers.c>
Header set X-XSS-Protection "1; mode=block"
</IfModule>
</VirtualHost>
HTTPS VHOST:
<VirtualHost *:443>
DocumentRoot /var/www/html/app/current/web
ServerName app.foobar
ServerAdmin me#app.foobar
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite>
Options Indexes FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !HTTP/1.1$
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
SSLEngine on
SSLProtocol +TLSv1 +TLSv1.1 +TLSv1.2
SSLCertificateFile /ssl/app.foobar/app.foobar.crt
SSLCertificateKeyFile /ssl/app.foobar/app.foobar.key
SSLCACertificateFile /ssl/app.foobar/app.foobar.ca-bundle
</VirtualHost>
As the title says when I use regular port 80 the urls are rewritten to use app.php but when I try to use https port 443 I get a directory listing instead. How do I get the url rewritten with https like it does with http?

I solved this by using the FallbackResource directive available in Apache 2.2.16+.
In the official Symfony docs this directive is not used to remain backwards compatible with older version of Apache. Discussion is here.
Code example:
<Directory /var/www/html/app/current/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
FallbackResource /app.php
</IfModule>
</Directory>

Related

How can I redirect traffic staging site without combining the domain prefix and suffix?

I've got a question about apache configs or it may be the .httaccess file. I have staging site : staging.witnesstolove.org with https enabled. when I try to go to the site, I'm redirected to stagingwitnesstolove.org and I'm not sure why?
My apache config seems pretty basic:
<VirtualHost *:80>
ServerAdmin accounts#1905newmedia.com
DocumentRoot /home/ubuntu/staging/witnesstolove/current/site/web/
ServerName staging.witnesstolove.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/staging/witnesstolove/current/site/web/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =staging.witnesstolove.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I see in devtools that i'm getting a 301 redirect and it says it's being redirected by wordpress.
If I look at the .htaccess file for the site, it's like this:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Based on the response headers I'm getting back, it sounds like wordpress is redirecting the traffic to the other domain?
Here's the HTTPS config for the site in Apache:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin accounts#1905newmedia.com
DocumentRoot /home/ubuntu/staging/witnesstolove/current/site/web/
ServerName staging.witnesstolove.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/staging/witnesstolove/current/site/web/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} =staging.witnesstolove.org
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/staging.witnesstolove.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/staging.witnesstolove.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Wordpress Site becomes 403 Forbidden Access and 404 not Found after installing SSL

my WordPress site becomes 404 not found after I install SSL in
sites-available/default-SSL.conf and this is the code :
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin my#website.go.id
ServerName website.go.id
DocumentRoot /var/www/website.go.id
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /home/website.go.id/ssl/website.go.id.crt
SSLCertificateKeyFile /home/ssl/website.go.id.key
SSLCertificateChainFile /home/simaled/ssl/DigiCertCA.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/var/www/website.go.id">
Options Indexes FollowSymLinks ExecCGI Includes
AllowOverride None
Require all granted
</Directory>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
I know you all probably wonder why i set the AllowOverride None . that's because when i set the AllowOverride to all all my pages become 403 Forbidden.
this is my .htacces code :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^lib/(.*) /wp-includes/$1?FJN_hide_my_wp=1234 [QSA,L]
RewriteRule ^user_submit /wp-comments-post.php?FJN_hide_my_wp=1234 [QSA,L]
RewriteRule ^wp-content/themes/jdih-child/screenshot\.png|readme\.html|license\.txt|wp-content/debug\.log|wp-includes/$ /nothing_404_404?FJN_hide_my_wp=1234 [QSA,L]
RewriteRule ^(index\.php|wp-comments-post\.php|wp-includes/js/tinymce/wp-tinymce\.php|xmlrpc\.php|wp-cron\.php|wp-admin/upgrade\.php|php-info\.php|sync-jdihn\.php|wp-login\.php|index\.php|wp-admin/|wp-content/plugins/)(.*) $1$2?FJN_hide_my_wp=1234 [QSA,L]
RewriteRule ^(.*)\.php(.*) /nothing_404_404?FJN_hide_my_wp=1234 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

How to config Apache to get Symfony4 routes working in subdirectory on a WordPress domain

On my Apache server I have a WordPress installation working on www.domain.com and I want to add a Symfony4 installation on www.domain.com/symfony. Symfony routes are taken to the WordPress 404 page (but still are the correct URL i.e. www.domain.com/symfony/about). However, if there are NO routes defined in my Symfony app, then the base www.domain.com/symfony/ URL will correctly display the Symfony4 getting started page. What is wrong in my Apache conf files?
Web server is Apache 2.4, I have tried using Alias and AliasMatch directives in the /etc/apache2 conf files. Alias displays WordPress 404 for all Symfony routes and AliasMatch appends many /index.php's onto the URL and displays the same 404. I tried to copy what phpmyadmin does with its Alias /phpmyadmin /usr/share/phpmyadmin
# /etc/apache2/sites-available/wordpress.conf
<Directory /var/www/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/wordpress
</VirtualHost>
# /var/www/wordpress/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# /etc/apache2/sites-available/symfony.conf
Alias /symfony /var/www/symfony/app/public
<Directory /var/www/symfony/app/public>
Options FollowSymLinks
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
# /var/www/symfony/app/config/routes.yaml
about_index:
path: /about
controller: App\Controller\AboutController::index
I expected www.domain.com/symfony/about to go to /var/www/symfony/app/public/index.php and route to App\Controller\AboutController::index, but it looks like WordPress is trying to find the page /symfony/about and 404's.
When I change my Alias /symfony /var/www/symfony/app/public to AliasMatch ^/symfony.* /var/www/symfony/app/public it changes the Symfony URL to www.domain.com/symfony/index.php/index.php/index.php/index.php/index.php/index.php but has the same result (WordPress 404 page). None of my changes have impacted the WordPress part at all - it still functions perfectly.
EDIT 1
Tried a new conf to test if it's my symfony config - it works as expected so I just think it must be something in my Apache confs. This config treats the symfony app as the main site and wordpress is totally disabled:
# /etc/apache2/sites-available/symfony_only.conf
<Directory /var/www/symfony/app/public>
Options FollowSymLinks
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/symfony/app/public/bundles>
FallbackResource disabled
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/symfony/app/public
</VirtualHost>
Looks like I got a working conf now. Not sure if it's even resulting in any different configuration than the one I had. I also did a lot of php bin/console cache:clear as I was even getting 404 and 500 errors on the little debug bar at the bottom of the symfony welcome page (not displayed on the bar, but the bar actually wouldn't properly load). The below file is my entire conf which I combined into one file. It serves from my Symfony4 app if the url path begins with /symfony, and the routes all work.
# /etc/apache2/sites-available/wordpress_symfony.conf
Alias /symfony /var/www/symfony/app/public
<Directory /var/www/symfony/app/public>
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/symfony/app/public/bundles>
FallbackResource disabled
</Directory>
<Directory /var/www/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also I added the symfony/apache-pack with composer which added this .htaccess in the symfony public directory. The docs said I can get improved performance by moving all these rules into my .conf file and disabling overrides:
# /var/www/symfony/app/public/.htaccess
DirectoryIndex index.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 ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>

.htaccess doesn't work on VirtualHost

Since I've setup a wildcard VirtualHost my htaccess files does not work anymore
The http-vhosts.conf
NameVirtualHost *
<VirtualHost *:80>
ServerName default.dev
VirtualDocumentRoot /Users/[UserName]/Sites/%-2
<Directory /Users/[UserName]/Sites/%-2>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
the .htaccess (WordPress default)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I can access http://wordpress.dev and also the backend but not a page like http://wordpress.dev/page (causes a 404 error).
I can set the permalink structure to default but I would like to have "nice URLs"
I'm on a local development environment on Mac OS X 10.10 with Apache 2.4
Change your VirtualHost definition to:
<VirtualHost *:80>
ServerName default.dev
VirtualDocumentRoot /Users/[UserName]/Sites/%-2
<Directory /Users/[UserName]/Sites>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
i.e, root the directory definition to Sites, not to the virtual directory.

wordpress urls not working with apache virtualhost

I have setup a apache virtual host using the below script -
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName nvoids.cu.cc
ServerAlias www.nvoids.cu.cc
DocumentRoot /var/www/html/blog
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now this is working fine at this url - http://nvoids.cu.cc
which serves the pages from /var/www/html/blog
But the wordpress permalinks are not working for example
http://nvoids.cu.cc/hello/ throws a 404 page.
For that i tried changing the .htaccess file as follows -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . blog/index.php [L]
</IfModule>
But its not working.
You need
AllowOverride All
In your VirtualHost directive to enable use of .htaccess files.
Is the following module enabled in your php.ini which should be uncommented? Do you see any error in your apache error log file?
LoadModule rewrite_module
As answered by Daniel the virtual server conf should be -
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName nvoids.cu.cc
ServerAlias www.nvoids.cu.cc
DocumentRoot /var/www/html/blog
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html/blog/>
AllowOverride All
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The .htaccess file as it is modified by wordpress -
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources