How to use a rewrite rule to force calls for "domain.tld/subdir/file.html" to show as "subdir.domain.tld/file.html"?

Posted by Wion on Stack Overflow See other posts from Stack Overflow or by Wion
Published on 2010-12-31T11:52:46Z Indexed on 2011/01/01 11:54 UTC
Read the original article Hit count: 216

Hi! First time poster. Very new to mod_rewrite. I'm on a shared server and the context of this problem is with a virtual directory under my root account.

The domain (domain.tld) will have subdirectories representing annual mini-sites of static .html files. Subdirectory names (yyyy) will be the 4-digit year (e.g., "2010").

I want any call to domain.tld/yyyy/file.html to appear as yyyy.domain.tld/file.html in the browser address bar, and (of course) for the page to load properly.

I already force dropping “www” by using…

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

So far so good.

But no matter what I try after that, I can’t get the subdomain to force to the front of the domain.

Here’s one of the more complicated examples I’ve tried (no doubt wrong)…

RewriteCond %{HTTP_HOST} ^domain\.tld/([0-9]+)/([a-z-]+)\.html [NC]
RewriteRule (.*) %1.domain.tld/%2.html [NC]

This doesn’t break anything (that I can tell), but it doesn’t do what I want either. I.e., if I type yyyy.domain.tld, I’ll see yyyy.domain.tld in the address bar, and navigating around will give me yyyy.domain.tld/file.html, etc. Fine. But if also type domain.tld/yyyy I’ll see domain.tld/yyyy, etc, which is not how I want people to see it. It doesn’t redirect or mask or alias or whatever you call it.

Is it even possible to force one look over the other like that? Should I be handling this with DNS instead?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about .htaccess

Related posts about mod-rewrite