Question

I have set up a static domain alias to serve static content without cookies, however as this is just an alias of the main domain I'm concerned that if it gets linked to inadvertently then it might be indexed. I would like to prevent the static domain alias from serving anything but CSS, JS, JPG, GIF files etc by way of a conditional in the main site .htaccess file.

i.e. something like:

If domain name includes the term 'static' then redirect all requests for files 'not' ending with .css, .js etc to the parent domain.

Any ideas?

Was it helpful?

Solution

In the document root of the static domain, add this to the htaccess file (preferably closer to the top, or above any existing rules):

RewriteEngine On

# check if hostname includes "static"
RewriteCond %{HTTP_HOST} static

# and the request isn't for a file ending with one of these extensions
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gid|png)$ [NC]

# then redirect whatever the request is to the parent domain
RewriteRule ^(.*)$ http://parent.domain.com/$1 [L,R=301]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top