Forum Moderators: phranque

Message Too Old, No Replies

URL modification of the 'www'

htaccess

         

ThaSpY

5:18 am on Sep 24, 2005 (gmt 0)



I'm looking for a simple htaccess script that does the following:

If the user visits my domain, I want to force them to use 'www.domain.com', and not simply 'domain.com'.

Simultaneously, if the user visits a subdomain of my domain, I want to force them to use 'subdomain.domain.com' and not 'www.subdomain.domain.com'

Basically, if there is no subdomain, force www. If there is subdomain, take away www.

Simple, right? Anyone got any pointers?

hyperblack

7:37 am on Sep 24, 2005 (gmt 0)

10+ Year Member



>> If the user visits my domain, I want to force them to use 'www.domain.com', and not simply 'domain.com'.

check this thread out [webmasterworld.com...]

and BTW, Welcome to WebmasterWorld (its always a pleasure for a newbie to welcome another newbie :))

ThaSpY

7:21 pm on Sep 24, 2005 (gmt 0)



Thank you, that solved the first part of my problem.

Now all I need is the subdomain redirections, as stated in my first post.

jdMorgan

10:07 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please see our forum charter [webmasterworld.com].

Post your best-effort code, and we'll help you get it working.

Jim

ThaSpY

10:50 pm on Sep 25, 2005 (gmt 0)



For the subdomain redirect, the only way I can think of doing it (because my knowledge is very limited on htaccess), is maybe to do a wildcard search to see if HTTP_HOST contains three periods in it (in which case finding something of the sort of www.subdomain.domain.com, because I'll only ever have one level of subdomain). If it finds this, then get htaccess to take off the first four characters in HTTP_HOST, and redirect it to there (taking off the 'www.'). If less than three periods are found, it's all fine, and then just do the toplevel domain check (domain.com -> www.domain.com, which I've alreasdy successfully done).

Like I say, I have no idea what I'm doing, but I'm giving my logic a shot. If I'm on the right track, I'll try to write something up. If not, perhaps you could suggest a method in a similar descriptive way as above, and I'll give it my best shot. Thanks.

jdMorgan

4:31 am on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the links in our charter to dissect and analyze the following. Really. It's important to understand every bit of mod_rewrite code you add to httpd.conf or .htaccess. That's because it's very compact and very powerful; One typo can change everything, and might -if you're lucky- take your server down. If you're unlucky, it'll sit there and cause a very subtle error only twice a year, and drive you to madness... (Not really, I hope). :)

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]

When testing, keep two things in mind:
  1. Flush your browser cache after *any* change to server-side code.
  2. The server error log is your best friend.

Jim

ThaSpY

5:13 am on Sep 26, 2005 (gmt 0)



Thank you for your reply.

www.domain.com works
domain.com works
sub.domain.com works
www.sub.domain.com gives me a 404

I actually spent the afternoon re-learning regex, and that part of your code makes sense to me. I'm slowly grasping the htaccess terms as well, so by all means, your code should work in my mind.

I'm still working on understanding backreferences, so I can't really debug that part of your code yet, but I'm thinking something there is causing an error?

ThaSpY

6:00 pm on Sep 26, 2005 (gmt 0)



Here's a pickle for you.

I'm currently using some version of cpanel, and it seems to have a weird way of setting up subdomains (one I'm not too particularly fond of). It basically maps sub.domain.com => domain.com/sub, creating a subdirectory off of public_html. So I did some altering to your code and this is what I found:

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

When I go to a subdomain like www.sub.domain.com, the above code redirects the page to example.net/sub/extrastuffhere. That is why the original script is producing a 404: sub.domain.com/sub doesn't exist.

So here's yet another topic I'm not too experienced in. Is there a way to tell apache to use the standard /subdomains outside of the /public_html so it doesn't automatically create domain.com/sub with every new subdomain? Or is there a way to alter the code so the variable $1 doesn't contain the subdomain in it during its redirection? I'm leaning towards the latter solution if possible.

[edited by: jdMorgan at 2:03 am (utc) on Oct. 2, 2005]
[edit reason] Example.net [/edit]

jdMorgan

9:28 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Control panels are just scripts that provide convenience for you (and protection for your host) at the cost of flexibility. You check or uncheck a box, and they add or delete some pre-tested lines of code in the httpd.conf file on the server. But they have their limitations, and this is one of them.

Your choices are either to by-pass the control panel and do the (sub)domain-to-directory mapping yourself, or to set up subdirectories for each subdomain that you want to use. This latter option can get unworkable fast if you have many subdomains -or worse- if you want to create them dynamically.

Look into you control panel documentation, and see if it is possible to map *all* (sub)domains to one single directory. If that is possible, you'll be able to use the code posted in several threads [google.com] here to 'sort them out' once you get them all pointed to an .htaccess file in the main domain's root directory.

Jim