Alex
Alex

Reputation: 809

Empty %1 in RewriteCond using mod rewrite

I got some domains with same name but different TLDs - let's say example.com and example.org. I want to redirect all requests from [www.]example.com to www.example.org as to use only one main domain.

I've found following code I'm using in my .htaccess file.

RewriteCond %{HTTP_HOST} !^www\.(.*)\.org$     [NC]
RewriteRule ^(.*)$       https://www.%1.org/$1 [L,R]

Problem is: it redirects to https://www..org/. Seems like variable %1 is empty although it should contain the domain name ("example").

As I understand Apache's httpd documentation it should work like that.

What am I missing?

Upvotes: 2

Views: 227

Answers (1)

Croises
Croises

Reputation: 18671

You can use:

RewriteCond %{HTTP_HOST} !^www\..+\.org$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.(?:.+)$ [NC]
RewriteRule ^ https://www.%1.org%{REQUEST_URI} [NE,L,R=301]

Empty %1 because in your case you use ! for a negative test, Which does not correspond to...

Upvotes: 2

Related Questions