0

If a subdomain isn't in my URL (or its "www") I'm trying to force an underscore as the subdomain. I went round in circles yesterday trying to understand this, here's what I have so far:-

Rule #1. Remove file extensions - works, until I add rule 2 to .htaccess.

Options -Multiviews
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

Rule #2 If there is no subdomain present automatically use an _ eg: _.mysite.com

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

Rule 2 works, but it's adding the .php back onto the end of the filename?

I've tried moving the "_" rule above the extension rule but it doesnt make a difference.

Would be greatful for any help =)

Will
  • 309
  • 1
  • 4
  • 10

1 Answers1

0

To remove ".php" extension on second rule you have to add ".php" as a optional parameter:

RewriteRule ^(.*)(\.php)|(.*)$ http://_.%{HTTP_HOST}/$1$3 [L,R=301]

This way, no matter your URI have ".php" or not your request will always be redirected without ".php" extension.

lomantpa
  • 56
  • 4