Forum Moderators: phranque

Message Too Old, No Replies

How to redirect html and php only from subdomains

         

smithaa02

5:37 pm on Jan 12, 2012 (gmt 0)

10+ Year Member



For mysite.com, I created a number of aliases.

eg css.mysite.com, images.mysite.com and js.mysite.com

Just found out google crawled these sites and I'm worried about duplicate content.

Is there a way to ensure that no html or php page renders under these special subdomains? So if images.mysite.com/testpage.html comes up it would auto-301 redirect to www.mysite.com/testpage.html?

lucy24

7:21 pm on Jan 12, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes.

But you've been around long enough to know that this isn't a we'll-write-your-code-for-you forum. Unless you've been so phenomenally useful in some neighboring forum that someone here owes you a favor ;)

You're looking at a bona fide redirect, which is simpler and safer than a rewrite. What does your code look like so far? Config file (if it's your own server) or htaccess (if shared).

How is the aliasing done? You'll want to work with it rather than against it. If you use mod_alias for anything, you will have to figure out if you can do the new stuff as straight redirects (no conditions). Otherwise everything has to move over to mod_rewrite to make sure the different steps happen in the right order.

smithaa02

8:49 pm on Jan 12, 2012 (gmt 0)

10+ Year Member



I would love to use something similar to the standard www redirect like:

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

But simply to do this for a subdomain and filter the rule by *php and *.htm* .

Fixing this should just be a simple .htaccess line... Anybody know how to do this?

lucy24

11:47 pm on Jan 12, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Subdomains count as hosts. So it's the same condition; you just add the subdomain names. Or use the vanilla redirect that goes

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

This will get you everything in one fell swoop by also picking up without-www forms and anything with a trailing port number. The ()? "or nothing" option is for, uhm, is it HTTP 1.0 that doesn't send a hostname? Put the rule after all your specific redirects if you've got any.

Constraining the Rule to .php and .html extensions is something you'd want to do in any case, so Apache doesn't have to stop and evaluate the Conditions for every single request, including images and stylesheets and javascript and so on. So you simply put that in the Rule:

([^.]+\.(php|html?))$

where you now have

^(.*)$

--which is redundant anyway. If you don't care what you're capturing, the anchors are unnecessary. By default, Regular Expressions start as soon as they can and go on as long as they can. And you don't want * giving the option of "nothing" because that would exclude those very extensions that you're looking for.

But wait! Are there any index files involved? Those could come through as requests ending in / or even an empty request if it's the top-level index.php or index.html. If so, you have to tweak the wording of the Rule a little more.

g1smd

7:44 am on Jan 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For bare folders you'll need a second rule. This rule will need to be listed last.

For named index pages you'll need a third rule, otherwise those requests will see an unwanted two step redirecion chain. This rule will need to be listed first.