Forum Moderators: phranque

Message Too Old, No Replies

Complicated htaccess subdomain 301 redirect?

         

internetheaven

10:08 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not having much success. Of course, I'm not very good at my htaccess stuff! What I'm trying to achieve is that any domain that encounters this htaccess file is stripped of any subdomin and put in to a www. format.

e.g. blog.domain.co.uk, forum.domain2.com and sub.domain3.com

would be turned in to:

www.domain.co.uk, www.domain2.com and www.domain3.com

as there are various domains virtually hosted and I use a script to manage them.

This is what I got from putting together what I've done elsewhere, but it doesn't work:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^((.+)\.|$) [NC]
RewriteRule ^ http:// www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

(Obviously without the break in the "http:// www", WW won't let me enter that as a solid piece!)

Like I said, the domain that calls this htaccess file will vary, but all subdomains need to be stripped and replaced with www.

Any ideas?
Thanks
Mike

jdMorgan

11:21 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+\.)+([^.]+\.(com|co\.uk))\.?(:[0-9]+)?$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]

Jim

[edited by: jdMorgan at 3:08 pm (utc) on Feb 12, 2010]

internetheaven

8:21 am on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but that didn't seem to do it. The non-www's still redirect to the www. version but no subdomain would redirect.

internetheaven

10:27 am on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Someone recommended this:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+\.)+([^\.]+\.(com|co\.uk))\$ [NC]
RewriteRule ^(.*) http://www.%2%{REQUEST_URI} [R=301,L]

which seems almost the same, but that hasn't worked either. There is another code in the htaccess file:

RewriteRule ^(.*) index.php

so that all files and folders hit the /index.php file for processing just like I want the subdomains to do. Could that be interfering?

It is placed underneath the code I'm trying for subdomains in the htaccess file.

[edited by: jdMorgan at 2:48 pm (utc) on Feb 11, 2010]
[edit reason] de-linked [/edit]

jdMorgan

2:54 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The redirect code I provided must precede the internal rewrite to "index.php".

I won't comment on the other suggestion, as it obviously cannot work.

The main questions are, is your DNS set up to point these subdomain requests to your server, and is your server configured to 'deliver' these requests to the filepath where this code resides?

In addition, are you sure that you don't have any code "written" by your "control" panel into your server config file that might interfere with what you're trying to do here?

If not, then the problem is likely outside this code, and you'll need to address these other issues first.

Do be sure that you are completely flushing (deleting) your browser cache before testing any changes to server-side code.

Jim

internetheaven

9:20 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main questions are, is your DNS set up to point these subdomain requests to your server


I thought they were! I missed a full stop in the wildcard setting.

The htaccess code still didn't seem to work, but the index.php was easily adjusted once the subdomains were seeing it.