apache - .htaccess - Remove www, force https, remove php and remove trailing slash -


i trying archieve following least amount of redirects;

  • remove www
  • force https
  • remove php extension
  • remove end trailing slash

what have far , working:

rewriteengine on  # remove www rewritecond %{http_host} ^www\.(.*)$ [nc] rewriterule ^(.*)$ http://%1/$1 [r=301,l]  # force https rewritecond %{http:x-forwarded-proto} !https rewriterule ^ https://%{http_host}%{request_uri} [l,r=301]  # remove trailing slash rewritecond %{request_filename} !-d rewriterule ^(.*)/$ /$1 [l,r=301]  # remove php extension rewriterule ^(.+)\.php$ /$1 [nc,l,r=301] rewritecond %{request_filename}.php -f rewriterule ^(.+)/?$ /$1.php [end] 

current behaviour:

http://www.example.com/functions.php -> https://example.com/functions 

(works 4 redirects)

or

http://www.example.com/functions/ -> https://example.com/functions 

(works 4 redirects)

does have suggestions how make work fewest possible redirects?

it doesn't hurt rewrite https , lose www in 1 step. trailing slash unchanged, lost line on removing php-extenstion inverting cond.

# remove www & force https rewritecond %{http_host} ^www\.(.*)$ [or] rewritecond %{https} off rewriterule ^(.*)$ https://example.com/$1 [r=301,l]  # remove trailing slash rewritecond %{request_filename} !-d rewriterule ^(.*)/$ /$1 [l,r=301]   # remove php extension if there's no file name rewritecond %{request_filename} !-f rewriterule ^(.+)\.php$ /$1 [nc,l,r=301] 

if need handling files requested file/ without .php, should stay code final part:

rewriterule ^(.+)\.php$ /$1 [nc,l,r=301] rewritecond %{request_filename}.php -f rewriterule ^(.+)/?$ /$1.php [end] 

Comments