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:

So, I want that all requests to

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.

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

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?

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?

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).

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$.

Horray!

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

Why not (as above):

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

Changing the domain, the code should be the same:

RewriteEngine on
[COLOR="#A9A9A9"]RewriteCond %{HTTP_HOST} ^www\\.mradlmaier\\.bplaced\\.net$ [NC] OR [/COLOR]
RewriteCond %{HTTP_HOST} [COLOR="#A9A9A9"]^mradlmaier[/COLOR]\\.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

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 :slight_smile:

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?

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

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

Me, too! Good Luck!

Regards,

DK

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.

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?

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?

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:

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

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?

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:

<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.

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

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

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

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