lospicos
lospicos

Reputation: 301

htaccess rewrite if subdomain is not present

I have a subdomain created to use as cookiless domain which is like cdn.domain.com

And on the main site I have www.domain.com. Whatever I call it comes as www.cdn.domain.com. I am using cdn.domain.com to call images etc.

Here you can see actual htaccess setting

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond $1 !^(index\.php|images|robots|public|sitemap.xml|favicon.ico|\.txt|\.html)
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
</IfModule>

How I can get https://cdn.domain.com without affectting any change to main domain settings?

Upvotes: 1

Views: 75

Answers (1)

halfpastfour.am
halfpastfour.am

Reputation: 5933

I think you're looking for this:

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

Your htaccess would look like this now:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond $1 !^(index\.php|images|robots|public|sitemap.xml|favicon.ico|\.txt|\.html)
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
</IfModule>

Upvotes: 2

Related Questions