user1334414
user1334414

Reputation: 11

htaccess remove file extension for domain and subdomains (eg. example.com and test.example.com)

I have a root domain www.example.com, and a subdomain test.example.com. I'm trying to remove file extensions - so far I can remove the file extension for the root domain but not the subdomain - it throws a 404 not found error. Below is my .htaccess code that is in the root folder:

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

For instance, I can type in www.example.com/page and it will show www.example.com/page.php, but if I type in test.example.com/some-page, it returns the 404 - I need to type in test.example.com/some-page.php for the page to show.

I'd also like to redirect all www. request to the non www. domain:

www.example.com -> example.com www.test.example.com -> test.example.com

Upvotes: 1

Views: 697

Answers (1)

Tomasz Banasiak
Tomasz Banasiak

Reputation: 1560

You need to set wildcard on your domain. If not set you need to put your .htaccess in every subdomain folder.

Rules to rewrite "www" to non-www addresses:

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

Upvotes: 0

Related Questions