Reputation: 33
I have been stuck on this for ages. I can't seem to get httpd.conf to rewrite URLs to add https (if it is not being used) and remove the .php extension. I can get either to work but not both at the same time. https://example.com/index gives a 404 error when index.php exists.
So what I am trying to do is turn URLs such as example.com/index.php into https://example.com/index
If I turn off the force HTTPS rule, it works.
httpd.conf:
RewriteEngine On
RewriteRule ^(.*)$ $1.php [QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule $ https://example.com/ [L,R]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
I have added multiple rules such as:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
And:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
But to no avail.
Any help would be great.
Upvotes: 3
Views: 706
Reputation: 19016
This should be working as expected.
For your http
block
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^/(.*)$ /$1.php [L,QSA]
RewriteCond %{HTTP_USER_AGENT} libwww-perl
RewriteRule ^ – [F]
For your https
(ssl) block
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^/(.*)$ /$1.php [L,QSA]
RewriteCond %{HTTP_USER_AGENT} libwww-perl
RewriteRule ^ – [F]
Note: i suggest you to use a htaccess in root folder (ssl document root folder must be the same as "http" domain). This way, you won't have split/duplicate code
Upvotes: 2