Sample Header Ad - 728x90

URL rewriting for root of website

1 vote
2 answers
475 views
This is the configuration of my Apache 2.4.37 webserver on Centos 8. File /etc/httpd/conf.d/mysite.conf: ServerName mysite.com DocumentRoot "/var/www/html" RewriteEngine on RewriteCond %{SERVER_NAME} =mysite.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] File /etc/httpd/conf.d/mysite-ssl.conf: 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 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?
Asked by dr_ (32068 rep)
Nov 20, 2020, 04:48 PM
Last activity: Nov 25, 2020, 12:17 PM