Frankie
Frankie

Reputation: 596

What's wrong with this .htaccess file?

I have a partially working .htaccess file and can’t for the life of me figure out what’s wrong.

Here’s the goal: I have example.com whose canonical form I want to be www.example.com. I have that working okay. I also have a subdomain located in the folder /lang/chinese which I want to resolve as china.example.net. This works fine too. Lastly I have the (parked) domain example.net, which I want to be redirected to example.com and to resolve therefore as www.example.com.

It’s this last part that doesn’t work. If I put www.example.net in my browser, that stays in the address bar.

Here’s the relevant portion of my .htaccess file:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} china.example.com [NC]
RewriteRule ^(.*)$ http://china.example.com$1 [L,R=301]


RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/lang/chinese/(.*)$ http://china.example.com/$1


RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]

Clearly I am doing something wrong here. How can I fix this?

Upvotes: 0

Views: 58

Answers (1)

Harvtronix
Harvtronix

Reputation: 854

For this rule:

RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]

the ^ is telling the regex to match against the beginning of the line. So that means you’re never actually searching for “www” there.

I believe you’ll want to change that to:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [nc]

Info from Apache:

Upvotes: 1

Related Questions