Home  >  Q&A  >  body text

Combining HTTPS redirection with rewrite rules cannot add .php extension

Hope someone can help. I want to remove the file extension (.php) from internal links, but of course I need to configure it in htaccess first.

I believe the correct code is

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]

However, when I add this code below/above the pre-existing code (forcing https), nothing happens, i.e. I still need to put the .php Enter the URL.

After adding the above code, the htaccess file will look like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://mysite/ [R=301,L]

Any ideas why this doesn't work?

Thanks

I've been searching online but all posts say to just add the rewrite code to htaccess, unfortunately this doesn't work.

I don't think this code plays well with the existing code that forces https?

P粉098417223P粉098417223171 days ago281

reply all(1)I'll reply

  • P粉194919082

    P粉1949190822024-04-05 12:23:24

    I think RewriteRule is too complex. I use the following for all my sites:

    # append .php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule .* rrreee.php [L]
    
    # fallback to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php [L]

    reply
    0
  • Cancelreply