SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple redirect/rewrite with .htaccess

    My goal is something very simple, but I dont get it:

    Lets say I have a site at http://somesubdomain.somedomain.com

    Now I have my own domain registered:

    http://mydomain.com

    So, I want that all requests to

    http://somesubdomain.somedomain.com
    http://www.somesubdomain.somedomain.com

    are redirected/rewritten to:

    http://www.mydomain.com

    Further more, all requests to

    http://somesubdomain.somedomain.com/somequerystring
    http://www.somesubdomain.somedomain.com/somequerystring

    should be redirected/rewritten to:

    http://www.mydomain.com/somequerystring

    In other words, I want that the host part (either "somesubdomain.somedomain.com" or "www.somesubdomain.somedomain.com") of the URL is replaced with "www.mydomain.com"
    The original host part should not be visible to the user in the his browser's address bar.
    Basically, for the user, the URL in his address bar should always look like "http://www.mydomain.com/somequerystring"

    There is a myriad of information (and misinformation, too) on the internet, so even after extensive search, I dont have a clue.

    I dont even understand if that what I aiming for is a redirect or rewrite?

    Can anyone tell me the correct code snippet?

    Tx.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    I thought you just asked this in another thread?

    Simply use a mod_rewrite statement which matches the {HTTP_HOST} variable and redirect everything to your new domain.

    Sorry, your last line had the look and feel of a "script kiddie" request so I'll refer you to the tutorial linked in my signature for you to learn how to do this (easy with the hint above). If you don't get it quickly, come back and show your code and I'll be happy to help you through to working code.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I asked it in another thread, but with a difference: I moved the Joomla site from the subdirectory to the root directory to get rid of at least one problem.

    However, I read through your tutorial:
    2 things I have stumbled across:

    1. Testing if mod_rewrite is enabled:
    I uploaded a file containing this simple line: <?php phpinfo(); ?>
    However, this displays a somewhat different version of the usual phpinfo, which doesnt even contain a apache2handler section.
    See for yourself:

    http://www.mradlmaier.bplaced.net/phpinfo.php

    2, After reading through your tutorial and assuming mod_rewrite is enabled (I contacted support about that in the meanwhile), I think this simple .htaccess should do the trick:
    [EDIT: I performed your test, in fact htaccess is not enabled, the original html file displays, so I hope support will enable htaccess]

    RewriteEngine on
    RewriteCond %{HTTP_HOST} www\.mradlmaier\.blaced\.net$ [NC] OR
    RewriteCond %{HTTP_HOST} mradlmaier\.blaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    Is this right?
    I am not sure if I can have to conditions tested with an OR, though?
    Last edited by mradlmaier; Jul 15, 2012 at 23:16. Reason: Performed your test

  4. #4
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, mod_rewrite is enabled and the test you propose returns the php file instead of the htm file.

    However I think the abov code is somewhat wrong, I think it should be:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.blaced\.net$ [NC] OR
    RewriteCond %{HTTP_HOST} ^mradlmaier\.blaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    Is this right?
    It doesnt seem to work... what I a missing here?

    [EDIT]

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.bplaced\.net$ [NC] OR
    RewriteCond %{HTTP_HOST} ^mradlmaier\.bplaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    But this doesnt work. While I am getting the right files displayed, the address bar never changes. I was thinking a 301 Redirect will cause the new URL displaying in the browser bar?

    Having the new URL displayed in the browser address bar is the goal. If it doesn't change, I dont konw for what I need the whole .htaccess thingie?
    My problem is NOT that, I dont get the right files displayed, but that I dont get the right URL displayed in the browser's address bar.
    Or is this a misconception?
    Last edited by mradlmaier; Jul 15, 2012 at 23:49. Reason: I mistyped the" bplaced"

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    mrad,

    Hmmm, IMHO, better to have continued that thread. No problem.

    Never post or link to phpinfo(); as the output gives too much information (saving a hacker a lot of time and effort).

    [EDIT: I performed your test, in fact htaccess is not enabled, the original html file displays, so I hope support will enable htaccess]
    Well, that scotches your code! Of course, the following SHOULD generate site-wide 500 errors (for what should be considered syntax errors for unrecognized commands).

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} www\.mradlmaier\.blaced\.net$ [NC] OR 
    RewriteCond %{HTTP_HOST} mradlmaier\.blaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]
    Well, I thought you were redirecting everything at blaced.net? Why not just test %{HTTP_HOST} against blaced\.net$ [NC]?

    I am not sure if I can have to conditions tested with an OR, though?
    I'm not sure what you're asking. Use of [AND] is assumed so, if you're trying to match A OR B (as you were above), then the [OR] flag is required. Note that the default AND cannot match both www.anything$ and anything$.

    Quote Originally Posted by mradlmaier View Post
    OK, mod_rewrite is enabled and the test you propose returns the php file instead of the htm file.
    Horray!

    However I think the abov code is somewhat wrong, I think it should be:
    Why not (as above):

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.blaced\.net$ [NC] OR 
    RewriteCond %{HTTP_HOST} ^mradlmaier\.blaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]
    Changing the domain, the code should be the same:

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.bplaced\.net$ [NC] OR 
    RewriteCond %{HTTP_HOST} ^mradlmaier\.bplaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]
    IF both domains are co-located, then just \.net$ would do the same thing!

    But this doesnt work. While I am getting the right files displayed, the address bar never changes. I was thinking a 301 Redirect will cause the new URL displaying in the browser bar?
    Correct! The R=301 will force the browser to display the redirection (as will the http://www.mradlmaier.com in the redirection).

    Having the new URL displayed in the browser address bar is the goal. If it doesn't change, I dont konw for what I need the whole .htaccess thingie?
    Aw, that's not always the case, ergo, the option to use http:// and the R=301 flag. It depends upon your needs. Have another look at the coding samples for ideas.

    My problem is NOT that, I dont get the right files displayed, but that I dont get the right URL displayed in the browser's address bar.
    Or is this a misconception?
    As above, that's NOT a misconception with the correct code (which you showed).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, some quick comments, more on this later

    The OR should then be [OR]
    I was not sure about the correct syntax, and sure, [AND] will never be true

    However, I contacted support of bplaced.net, and they had a totally different take on that.
    They said, the domain must not be redirected at my dns provider but have a CNAME entry instead. And then I would not need a .htaccess at all.
    Luckily, I im full charge of my DNS as I am with united-domains.de. Unfortenately, and thats how the problem started, is, that I have no clue how to administer DNS entries. That is kind of unusual, because normally the host provider registers the domain name and administers the DNS as part of the hosting plan.
    This time around, I decided against that because, I was keen on learning all the DNS stuff (and save a few bucks).
    The control panel of united-domains.de is a bit misleading on that, because it prominently displays the redirect to a the host, and somewhat burries the CNAME entries.
    So, I changed that now, and once the changes are propagated through the Internet (72 hours max.), I will see if their advice is correct.

    What is your opinion?

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    mrad,

    Correct, flags must be enclosed in square brackets; multiple flags must be separated ONLY by commas (no spaces).

    Aha! You got my point on the logic of A AND B when they're mutually exclusive (because of the start anchor).

    CNAME record? I would have thought an A record would have been correct but I'm NOT an expert in DNS settings (I consider myself to be dangerous in that realm).

    cPanel's WHM allows changing the DNS settings so I have successfully edited that to point to a different server (for a subdomain for a client). That's completely removed that subdomain from my hosting. The CNAME record was probably used because it was a subdomain while my very limited understanding is that domains need A records.

    My domain with an SSL attached has several entries:

    SOA with the domain name, 2 Name Servers (one for each "private name server", an A record with the IP address, an ftp record with the IP address and CNAME records for www and mail citing the domain name.

    Not much to go on but that seems to belie what your host told you. If they're wrong, they should help you set it up correctly.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I only know must be a name and A must be an IP. The thing is because usually the providers do care this stuff, there is not much usable info around. My idea was that if I can figure it out, I will have a free host with 8! MySQL db (yes, bplaced gives you you EIGHT MySQL db and 8 Postgres db on free accounts, but no cPanel or Plesk or any other control panel) and united-domain.de registers tld domains for 15 Euros including DNS hosting for 15 Euros --> That would be LAMP wiith 8 dbs, unlimited traffic and domain for 15 bucks a year!!! And turns out bplaced is very fast... hope it all pans out as planned

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Me, too! Good Luck!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  10. #10
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works, at least partly
    Now http://www.mradlmaier.com brings up the pages with http://www.mradlmaier.com (as intended) in the addressbar
    http:www.mradlmaier.bplaced.net brings up http:www.mradlmaier.bplaced.net with http:www.mradlmaier.bplaced.net in the addressbar. Probably this can be fixed with .htaccess (I am not using .htaccess now)

    Unfortunately http://mradlmaier.com brings up "can not be reached". As its not reached, this cant be fixed with .htaccess. I hope support comes up with an advice how to fix that.

    Anyway, all in all support was right, that it is a DNS issue in the first place.

    I keep you updated.

  11. #11
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.bplaced\.net [NC] [OR]
    RewriteCond %{HTTP_HOST} ^mradlmaier\.bplaced\.net [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    This ^www\.mradlmaier\.bplaced\.net matches all URLs which start with mradlmaier.bplaced.net . Correct?
    So the above should all requests to www.mradlmaier.bplaced.net or mradlmaier.bplaced.net (and query strings) and redirect them to www.mradlmaier.com (+query strings), right?

    But it doesn't work. The right pages are displayed, but the URLs in the addressbar of the browser doesn't change.

    What I am missing here?

  12. #12
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mradlmaier\.bplaced\.net [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    RewriteCond %{HTTP_HOST} ^www\.mradlmaier\.bplaced\.net [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]

    The code above does the trick. Although I don't understand why the version with [OR] does not work.
    Any explanation?

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    mrad,

    The tutorial Article linked in my signature will explain mod_rewrite for you.

    If you don't care about the leading www in the bplaced.net redirections simply use:
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} mradlmaier\.bplaced\.net$ [NC]
    RewriteRule .? http://www.mradlmaier.com%{REQUEST_URI} [R=301,L]
    That will not require something before mradlmaier (www. or nothing) but still match mradlmaier.bplaced.net (good to use the No Case flag). The rest is perfect.

    Your [OR] flag instance does not work simply because your flags must be within a single set of square brackets with multiple flags separated ONLY by commas (no spaces). In other words, your [NC] [OR] should have been [NC,OR].

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  14. #14
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I understand.
    Thanks.

    I have one more question:

    I have read, that there are rules for denying access to .htaccess. I was assuming, that Apache will per default deny access to .htaccess?

    Can you enlighten me?

  15. #15
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    mrad,

    Well, I can take a guess (because there are so many different installers and hosts will configure any way they want).

    My test server (Apache 2.2.? on Win7/64) has the following (default) in the httpd.conf:

    Code:
    <FilesMatch "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy All
    </FilesMatch>
    That will certainly do the trick to prevent access to the .htaccess (.ht{anything}) file and should be considered the best way to protect it. You could use file attributes, too, as long as you allow yourself to read and write to the file AND allow Apache to read the file - users should never be allowed to read, write (or execute) .htaccess.

    Code:
    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
    ... is another statement you'll see but I believe (I don't know for sure) that this is designed for Windows servers. Because the example of this code which I found preceded the first example above, I'd stick with that.

    Yes, you can also use mod_rewrite like

    Code:
    RewriteRule /.htaccess$ - [F]
    ... but I'd ask why you'd not use the <Files> (or <FilesMatch>) directive which is specifically designed to protect a file like this.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  16. #16
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    I dont have access to .httpd.
    But I tried to open http://www.radlmaier.com/.htaccess with the browser. I got the 404 Error Page of my host. So I am assuming that the .htaccess is sufficiently secured?

    Regards,
    Michael Radlmaier

  17. #17
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Michael,

    That would be a pretty good bet - although I'd have expected a 403 (forbidden; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a full list of status codes).

    Suggestion: Turn OFF your mod_rewrite code protecting the .htaccess file and try it again. I'd bet that the host has the server configured to protect .htaccess already. Don't forget to turn it back on as it's better to be doubly safe.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •