Forum Moderators: phranque

Message Too Old, No Replies

Force access through subdomain

block domain.com/subdomain from working

         

Improfane

7:33 pm on Jul 19, 2009 (gmt 0)

10+ Year Member



Hello there,

  1. I use rewrite rules for subdomains. My subdomains are 'catch-all' in the sense that they automatically redirect to a script.

  2. My forums.domain.com/ works fine. Unfortunately you can also get there from domain.com/forums and I do not like this.

  3. How can I adjust my current rewrite rules to permanently block or redirect [domain.com...] to the correct [subdomain.domain.com...] ?

  4. My rule file is as follows:

    RewriteEngine on
    # no-www level b compliance
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    # RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Custom no-www from all domains (to comply with no-www.org)
    RewriteCond %{HTTP_HOST} ^(www\.)+(.+)$ [NC]
    RewriteRule ^(.*)$ http://%2/$1 [R=301,L]

    # Catch-all script for user redirects
    RewriteCond %{HTTP_HOST} ^(^.*)\.inforumal.com$
    RewriteRule ^(.*)$ /home/impro/public_html/forums/user_redirect.php [L]

  5. I want to keep my no-www working and that any link to a page to the subdomain folder should return a 404 ideally or it will clutter search engine results?
    i.e, the link [subdomain.com...]
    should not just redirect to forums.subdomain or that will go in search listings.

  6. What is the best practice?
    At the moment, my [domain.com...] is in the search listings and I do not like this.

To help people searching these are what I searched:
  • Block access to subdomain folders
  • Redirect subdomain folder to subdomain
  • Hide subdomain folder
  • Subdomains not in search engine results
  • Redirect subdomain folder to subdomain permanently

My StackOverflow question [stackoverflow.com] may also help.

jdMorgan

7:57 pm on Jul 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best practice would be to 301-redirect (only) direct client requests for the subdomain-subdirectories back to the canonical subdomain URL.

There are two ways to do this: You can make a rule that 'lists' each subdomain-subdirectory, but this can be a maintenance nightmare. Or alternatively (and I think, better), you can 'tag' your subdomain-subdirectories, and then detect that 'tag' and redirect any client request for URL-paths starting with that tag.

For example,
1) Rewrite <subdomain>.example.com/<path-to-resource> to /sd_<subdomain>/<path-to-resource> (The 'tag' is "sd_")
2) Redirect direct client requests for <anything>.example.com/sd_<subdomain>/<path-to-resource> to <subdomain>.example.com/<path-to-resource>
and of course, you'll also want to preserve any query strings appended to those URL-paths.

The trick is in the phrasing "direct client requests only" above. By detecting and redirecting *only* direct client requests for the subdomain-subdirectories, you prevent the two rules from interacting and causing an 'infinite' rewrite-redirect loop. You can use the variable %{THE_REQUEST} in a RewriteCond to implement this.
The variable %{THE_REQUEST} contains the entire client request line, as shown in your raw server access log file, for example:

GET /sd_subdomain/some-directory-path/some-page.php?query=foo HTTP/1.1

Jim

Improfane

11:21 pm on Jul 19, 2009 (gmt 0)

10+ Year Member



I want to ignore the part after /subdomain/

including query strings. It should be 'invalid' to get to subdomains content through the folder. I cannot get:


RewriteRule /subdomain http://subdomain.domain.com/ [R=301]

To work. Any ideas why this wouldn't work? I only want to do this for one particular subdomain.

jdMorgan

2:29 am on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have already offered my best advice from an HTTP protocol and SEO standpoint above.

Your rule likely doesn't work because it creates the loop I previously warned about.

Jim

Improfane

12:05 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



I am sorry but do not really understand...

I think this is what I understand:

  • Rename my forums folder to something besides forum like sd_forum

  • ..then redirect forums.example.com/ to sd_forum from CPanel interface

  • I use THE_REQUEST to detect a sd_forum or forums path and permanently redirect them to forums.example.com (and ignore the path)

  • This stops an infinite loop because they both have the same folder name (and nobody should know the tag)
  • g1smd

    1:24 pm on Aug 21, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    This stops the loop as the redirect only happens when a client directly requests that sort of URL.

    The redirect does not happen when the internal path has been set by a previous internal rewrite, as Jim explained above.

    Improfane

    1:30 pm on Aug 21, 2009 (gmt 0)

    10+ Year Member



    I have solved the problem. Here's what I did:

    jdMorgan's solution was either:

    a) redirect the subdomain to a new URL and make sure the document root is the same for ANY subdomain subdomain.example.com/ to subdomain.example.com/subdomain
    (this is how I understood it)
    or
    b) redirect when the server asks for the subdomain folder directly and make sure they get sent to the subdomain

  • I put following .htaccess into my subdomain folder:

    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule ^(.*)$ http://subdomain.example.com/ [L,R=301]

    It checks to see if the user is accessing the folder from the domain or the subdomain. It sends a moved permanently.

  • Inside my root .htaccess I have the following:


    # Ensure that user is on subdomain
    RewriteRule ^subdomain(/?)$ http://subdomain.example.com/ [L,R=301]

    This forces people that try get to the domain.com/subdomain onto the subdomain

    Outcome:

  • subdomain.example.com goes internally to /sd_subdomain/
  • example.com/ goes to internally /
    and
  • example.com/subdomain/ redirects to subdomain.example.com/ (ignoring path)
  • example.com/sd_subdomain/ redirects subdomain.example.com/ (ignoring path)
  • example.com/subdomain/path is invalid

    Which is just what I wanted. Thank you jdMorgan and g1. I may experiment with forcing error pages from the example.com/subdomain just to prevent them being used.

    Hopefully this helps someone else.

    Update: pasted wrong line

  • jdMorgan

    2:41 pm on Aug 21, 2009 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I'm surprised that that works, actually. It implies that requests for your subdomains are not processed through your root .htaccess file. So I assume that you've used a control panel to define these subdomains as "add-on domains" and that the root .htaccess file is not invoked when those add-on domains are requested by an HTTP client.

    Just for reference, the following rules are an example one of the "standard" implementations for the case where all domains and subdomains are mapped to the same DocumentRoot ("home page") directory. That is, where the subdomains are not set up as "add-on domains" using a control panel, but rather are mapped (using DNS only) to the same directory as the "main domain" on a server having a unique IP address. This illustrates the solution I outlined previously:


    # Externally redirect direct client requests for subdomain-subdirectory URLs
    # to subdomain URLs without subdomain-subdirectory URL-path
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sd_[^/]+/
    RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
    #
    # Externally redirect non-canonical domains and subdomains
    # (extra or missing "www") to canonical domain and subdomains
    RewriteCond www>%{HTTP_HOST} ^(www)>example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com\. [NC,OR]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com\.?:[0-9]+ [NC]
    RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
    #
    # Internally rewrite all but "main domain" URL-requests to
    # subdomain subdirectory filepaths unless previously done
    RewriteCond $1 !^sd_
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule ^(.*)$ /sd_%1/$1 [L]

    The code above assumes that all subdomains except "www" are hosted in subdirectories named "/sd_<subdomain-name>/" and that the canonical "main domain" is "www.example.com" -- That is, requests for "http://example.com/<anything>" will be redirected to "http://www.exmaple.com/<anything>"

    Additionally, the code will redirect if the requested hostname is in FQDN format, if it has a port number appended to it, or if it contains an "extra" www either before or after the "real" subdomain name. Since these are the most common non-canonical requests, this fixes and prevents most duplicate-content problems related to the requested hostname.

    Jim