0

I have a custom build non-CMS site on PHP.

The way my current .htaccess is setup is to allow me to use the urls without ".php" part and to 301 redirect all non www requests to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA] 

Now we are switching to HTTPS.

How do I get a proper SEO-friendly redirect from http:// to https:// in .htaccess?

I've tried replacing third string with

RewriteRule (.*) https://www.example.com/$1 [R=301,L]

but it doesn't do the trick...

Also, optionally, is there a way to set up a 301 redirect for .php request to redirect to non-.php variant? Although it looks like it means to redirect to same exact page, not sure if it will be SEO-safe to do it or maybe it will even result in a redirect loop?

Acidon
  • 1,294
  • 4
  • 23
  • 44

1 Answers1

0

You can use the following htaccess rules (just replace example\.com with yourdomain\.com in the second RewriteCond)

RewriteEngine On


#301 redirect from http to https + non-www to www
RewriteCond ℅{HTTPS} off [OR]
RewriteCond ℅{HTTP_HOST} ^example\.com$ [NC]
RewriteCond ℅{HTTP_HOST} (?:www\.)?(.+)$
RewriteRule ^ https://www.℅1℅{REQUEST_URI} [NE,L,R]

#remove .php extension
#redirect to remove .php from URLs
RewriteCond ℅{THE_REQUEST} /(.+)\.php [NC]
RewriteRule ^ /℅1 [L,R=301]
#handle extension less php URLs
RewriteCond ℅{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115