skip to Main Content

I hope somebody may be able to help. I want to remove file extensions (.php) from my internal links but of course I need to first configure this in htaccess.

I believe that the correct code is

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

however, when I add this code underneath / above the pre-existing code (to force https), nothing happens i.e. I still have to enter .php into the URL.

The htaccess file looks like this after adding the above code:

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

Any ideas why this wouldn’t be working?

Thanks

I’ve been scouring the web but all posts say to simply add the rewrite code to htaccess, which unfortunately isn’t working.

I think the code isn’t playing well with the pre existing code to force https?

2

Answers


  1. Try adding this snippet into your .htaccess file

    RewriteEngine On
    RewriteBase /custom/
    
    RewriteCond %{THE_REQUEST} /custom/(?:index)?(.*?).php[s?] [NC]
    RewriteRule ^ %1/ [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    Login or Signup to reply.
  2. I think the RewriteRule is to complex. I use the following for all my websites:

    # append .php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule .* $0.php [L]
    
    # fallback to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search