0
votes

I've been trying to use the .htaccess file to redirect www to non-www url of my website and hiding the .php file extension of pages.

Currently, the code below works in these situations:

  • going to www.domain.com and being redirected to domain.com
  • navigating pages, going from domain.com to domain.com/page

Currently, the code below does not work in these situations:

  • going to www.domain.com/page goes to domain.com/domain.com/page.php
  • going to www.domain.com/subfolder/page.php goes to domain.com/domain.com/subfolder/page.php
  • going to www.domain.com/page.php goes to domain.com

here's the .htaccess file:

DirectoryIndex /index.php

RewriteEngine on

# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

What can I do so that www.domain.com/page redirects to domain.com/page and is there a way to keep it generic to all subfolders?

Thank you

1

1 Answers

1
votes

Use this code

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