I have a strange issue with redirecting URLs in an edge case scenario. All of my applications using my framework use https with forced www and a trailing slash on the URLs. The https and forced www redirects are done with virtual hosts (i've included the normal apache2 conf and the certbot https generated ones). The redirection all works apart from a very edge case scenario when one letter is put at the end of the URL with no trailing slash where index.php gets added to the URL, for example:
example.com/aa redirects to example.com/aa/ correctly example.com/a redirects to example.com/index.php/a/ incorrectly
I can't seem to nail down what is causing this issue, but i'm sure its to do with the htaccess file as the 301 redirect (as seen in chrome network) happens before the request hits the code of the website. This happens on both my local and live environments.
Many thanks for all advice!
I've tried using different htaccess configurations with no luck (just made the situation worse).
example.com.conf:
# domain: example.com
# public: /var/www/example/public/
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/example/public
# Log file locations
LogLevel warn
ErrorLog /var/www/example/log/error.log
CustomLog /var/www/example/log/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
example.com-le-ssl.conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster@examplw.com
ServerName www.example.com
ServerAlias example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/example/public
# Log file locations
LogLevel warn
ErrorLog /var/www/example/log/error.log
CustomLog /var/www/example/log/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
</VirtualHost>
</IfModule>
.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
Expecting:
example.com/aa to redirect to example.com/aa/ example.com/a to redirect to example.com/a/
Images & files should redirect without the trailing slash.
Requests that already have the trailing slash should not redirect at all.