Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

.htaccess mydomain.com to www.mydomain.com redirect ?

  • 27-03-2006 11:49pm
    #1
    Closed Accounts Posts: 19,080 ✭✭✭✭


    I would like to redirect anyone who attempts to visit http://mydomain.com to http://www.mydomain.com

    I previously had some code which allowed me to do this using the .htaccess file but I seem to have misplaced it and can't find it again :(

    Because mydomain.com and www.mydomain.com both point to the same directory it's gotta be .htaccess and can't be a php redirect or what not.


    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 3,889 ✭✭✭cgarvey


    Assuming mod_rewrite is available to you, then the following from the Apache manual should work:
    # And for a site running on port 80
    RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R]
    


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    @cgarvey
    Cheers, I'll give that a bash a little later.


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    Time to bring this back up again, now that I've got a little more time.
    I don't follow your example above cgarvey. Could you break it down for me?

    Thanks


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    Er, do you have any subdomains which you wish to keep?

    http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
    RewriteEngine on 
    
    Turns the RewriteEngine on so the following will work...
    RewriteCond %{HTTP_HOST}   !^www\.yourdomain\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
    

    "\." - escape character == "." [Result being www.yourdomain.com ]
    The [NC] = 'not-case sensitive'
    !^www\.yourdomain\.com = if "www.yourdomain.com" was not typed in by the visitor...
    !^$ = ...not an empty string...
    The last line comes into effect if the previous conditions are met and states that they should be redirected to www.yourdomain.com


  • Closed Accounts Posts: 19,080 ✭✭✭✭Random


    forgive me - what should I type in place of "fully.qualified.domain.name" ? I try "www.mydomain.com" but no effect ?

    Also, what does [L, R] refer to? And is the $1 in the last one so that it will keep the exact url?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,889 ✭✭✭cgarvey


    cgarvey wrote:
    # And for a site running on port 80
    RewriteCond %{HTTP_HOST}   !^www\.domain\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://www.domain.com/$1 [L,R]
    

    The 1st line means the 3rd line will only be excuted if the host being accessed is not www.domain.com (e.g. domain.com). The 2nd line means the 3rd line (the rule itself) will only be applied if the host is acutally specified (HTTP 1.0 browsers won't pass a hostname, just HTTP 1.1 / virtual "shared" hosting use this host name field).

    The 3rd line then is that actual rule, which has 2 parts. The first part, "^/(.*)", simply matches any URL for that domain. The brackets mean that whatever is matched (everything, in this case) can be used as $1 in the second part.

    The NC (no case) makes the condition case-insensitive. The L means this is the Last rule to be applied (don't bother reading any other rules, if they exist). The R means issue a redirect (you might want to proxy the connection here, for example). The redirect is a 302 by default.

    More info ...
    Apache mod_rewrite docs
    URL Rewriting Guide


Advertisement