Home How to remove .php extension, redirect old files to new files and force SSL in .htaccess
Reply: 0

How to remove .php extension, redirect old files to new files and force SSL in .htaccess

user1693
1#
user1693 Published in June 19, 2018, 6:25 pm

I searched this on Stackoverflow and web many times but I couldn't find a one that does this all, that's why I'm asking this question.

For one of my new project, I decided to remove .php file extensions from URLS.

Using .htaccess

I want to

  1. Force SSL
  2. Redirect www URLs to non-www

  3. Remove .php file extension from my URLS

  4. Redirect old .php URLS to new non URLS
  5. Add trailing slash after URL /

This is what I tried but trailing slash and ssl is not working properly. I do not know if I'm using the right order.

To force SSL

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

WWW to non WWW

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Remove and trailing slash

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Redirect extension files to non extension.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.com/yourdirectory/$1 [R=301,L]

How to achieve this in the best way? Any input would be greatly appreciated!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346885 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO