I have 2 goals that I am trying to accomplish. 1st is to make sure the user is always using https and the 2nd is to get rid of .php extension entirely since every single one of my web page uses this.
My websites .htaccess file is wrong and I’m not entirely sure how to fix it.
It redirects from urls such as:
www.mysite.com/login.php to www.mysite.com/mysite.com/login
The 2nd broken url manages to eliminate all the PHP but concats my entire website within itself…
I have tried other htaccess “php” file extension removers found on various sites but either they do not work or only partially work (only some of my web pages get the .php removed). I’m think I must be including one extra thing that is not needed in my .htaccess but I’m very unfamiliar with configuring this file so every time I make a change its straight to the 500 error page…
Header add Strict-Transport-Security "max-age=31415926;includeSubDomains;"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# remove the .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
# redirect from .php to less php
RewriteCond %{THE_REQUEST} ^[A-Z]+ /([^/]+/)*[^.#? ]+.php([#?][^ ]*)? HTTP/
RewriteRule ^(([^/]+/)*[^.]+).php /$1 [R=301,L]
Anyone have any ideas?
2
Answers
To remove .php from URL:
Something like this:
Online Tester – unfortunately I don’t see a way to save the test. It’s a pretty nice tester, but it can’t handle the
!-f
condition so keep that in mind. Obviously some made up URL doesn’t actually exist on the testers domain.Anyway, when you put a URL like this:
The server cannot run that, what HTACESS does is behind the scenes it adds the
.php
extension and stopsL
. So the URL the server sees is thiswww.example.com/somepage.php
Provided the URL doesn’t exist as a real file or directory (before doing the rewrite rule).However, when the URL is like this
www.example.com/somepage.php
then HTACCESS removes the.php
and does a 301R=301
and stopsL
. After the redirect the first rule will then add.php
back into the URL but “Only” on the server so in the browser the url will not contain the PHP extension.Hope that makes sense.