Folks, you've got to help me - I am at my wit's end. I am an htaccess newb, and I've been through so many tutorials and forum topics but none of them seem to give a solution that works. Simply put, I want: sub.domain.com ...to redirect to: domain.com/sub ...where sub can be anything, not one of a predefined, hardcoded list of possibilities. I've tried all the following suggestions from various sources, and more, with no success: RewriteEngine On RewriteCond %{HTTP_HOST} ^[www\.]*[a-z0-9-]\.domain\.com [NC] RewriteCond %{REQUEST_URL} !^/$1/.* RewriteRule ^(.*) /$1/$2 [L] Code (markup): RewriteEngine On Options +FollowSymLinks +Indexes RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} !www\.domain\.com [NC] RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com [NC] RewriteCond $1 !page\.php$ RewriteRule (.*) /%2/page.php?username=$1 [L] Code (markup): RewriteEngine On RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-\_]\.domain\.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301, L] Code (markup): RewriteEngine On RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$ RewriteCond %{1} !^(www|example)$ RewriteCond %{REQUEST_FILENAME} !^/public_html/ RewriteRule (.*) /public_html/%1/$1 [Last] Code (markup): RewriteEngine On RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-\_].domain.com$ [OR] RewriteCond %{HTTP_HOST} ^www.[a-zA-Z0-9\-\_].domain.com$ RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] Code (markup): Some of these aren't even designed to do exactly what I want (like redirecting to a dynamic page rather than a directory), but I still can't get them to work. I'm sure each of them has its merits, but all I get is a "server not found" error in my browser. The only other line in my htaccess file is this, at the end: ErrorDocument 404 /404.php Could some kind soul please show me a working piece of code that I can cut and paste? This is starting to take years off my life.
Try these 2: RewriteEngine On RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] RewriteRule ^(.*)$ http://domain.com/%1 [QSA,R=301,L] Code (markup): RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.domain\.com RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com RewriteRule ^$ /%1 [L] Code (markup):
Thanks for your reply, evera. I've got round the problem a different way, which is probably not the best but I'm just happy that something finally works! By asking my host to enable wildcard subdomains, I've been able to catch errant URLs in the script of my index page and deal with them from there. Until then, requests for nonexistant subdomains hadn't even been reaching Apache, which is apparently why editing .htaccess was futile.
evera, they only catch the requests for those subdomains, it doesn't enable them. This can be done by using a server alias, or adding *.example.com in your control panel subdomain manager, or just asking your host nicely as you did
Ah that was the problem, i just thought you had some syntax issue. But when it works its fine I guess.
yeah, you gotta keep in mind that subdomains have to resolve through dns before they even see your apache server