This is the configuration of my Apache 2.4.37 webserver on Centos 8.
File /etc/httpd/conf.d/mysite.conf
:
<VirtualHost *:80>
ServerName mysite.com
DocumentRoot "/var/www/html"
RewriteEngine on
RewriteCond %{SERVER_NAME} =mysite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
File /etc/httpd/conf.d/mysite-ssl.conf
:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName mysite.com
DocumentRoot "/var/www/html"
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
ErrorDocument 403 /error403.html
ErrorDocument 404 /error404.html
ErrorDocument 405 /error405.html
ErrorDocument 500 /error500.html
RewriteEngine on
# First rule block
RewriteRule ^/$ /index.php [R,L]
# Second rule block
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*([^/.]+))\.php[\ ?]
RewriteRule \.php$ /%1/ [R=301,NC,L]
RewriteRule ^(.*)/$ /$1.php [NC,L]
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
TraceEnable off
</VirtualHost>
</IfModule>
The second rule block is taken from here and rewrites all URLs https://mysite.com/anypage.php
to https://mysite.com/anypage/
, hiding PHP file extensions and making permalinks nicer to see.
I've added the first rule block after noticing that the solution suggested in the link had a bug -- that is, URL https://mysite.com/
returned a File not Found. Now it works.
However, a minor annoyance is that https://mysite.com/
redirects to https://mysite.com/index/
(since it loads the file index.php
).
My question: How can this configuration be changed so that the URL https://mysite.com/
stays the same?
[L]
flag will only prevent the underlying rules from being applied in the current iteration. However, Apache iterates over and over again from the beginning until the URL stops changing. In this case (and in general), it takes more than one iteration, so your solution will not work. – mateleco Nov 23 '20 at 0:36http://localhost/
stays the same, andhttp://localhost/foo.php
redirects tohttp://localhost/foo/
. Maybe share the full code? – mateleco Nov 23 '20 at 19:58