skip to Main Content

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


  1. To remove .php from URL:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]+)$ $1.php [NC,L]
    
    Login or Signup to reply.
  2. Something like this:

    RewriteEngine On
    #force HTTPs
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    
    # add the .php extension (so the server can process it)
    RewriteCond %{REQUEST_FILENAME} !-d  #is not a directory
    RewriteCond %{REQUEST_FILENAME} !-f  #is not a file
    #We don't want to add PHP to things like .js or .png or .php files.
    #So if it's a real file we can stop here.
    #If it's not just add .php
    RewriteRule (.+) $1.php [L]
    
    # remove the .php extension and redirect to clean URL
    RewriteCond %{REQUEST_URI} .php$  #ends in .php
    RewriteRule (.+).php $1 [R=301,L]
    

    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:

    www.example.com/somepage
    

    The server cannot run that, what HTACESS does is behind the scenes it adds the .php extension and stops L. So the URL the server sees is this www.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 301 R=301 and stops L. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search