Forum Moderators: phranque

Message Too Old, No Replies

second htaccess file does not work

         

waseemni

12:17 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Hi,

I have 2 separate websites on the same server. Let's call them website1 and website2 :). On the server, the folder of website2 is a subfolder of the main folder of website1.
But, website2 has its own domain and it can not be accessed as website1.com/website2. So far so good.

We have moved website1 to a new domain and I have made redirects with the htaccess file that is in the root folder of website1. The problem now is that when I go to www.website2.com, it is also redirected to the new website.

I read on [another site] that the htaccess file closest to the folder being viewed will be the first one to be read by the server. So, I placed an empty .htaccess file in the folder of website2. But the redirect is still working on www.website2.com.

Can anyone advice how can I solve it or what am I doing wrong.

I'm very new to htaccess, so excuse any possible silly question.

[edited by: jdMorgan at 4:34 pm (utc) on Apr 16, 2010]
[edit reason] No URLs, please. See Terms of Service and Charter. [/edit]

jdMorgan

4:39 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The .htaccess file closest to root gets read first, but lower-level .htaccess files can override configuration settings for requests which reach those lower-level directories without being rewritten by the higher-level .htaccess file(s).

Your domain redirect code is likely deficient, in that it is not testing the HTTP_HOST variable to see what domain is being requested before it redirects the request over to the new domain. Therefore, all requests to this server are being redirected.

Of course, we can't be sure if we can't see the code here...

Jim

waseemni

9:31 am on Apr 17, 2010 (gmt 0)

10+ Year Member



Hello Jim,

yes, you are right. The htaccess does not check which domain is checked. Can you redirect to a rosource about it?

I built my htaccess and based on a tip of yours 2 weeks ago.

the file is just a big list of specific redirects and this a piece of it:

redirect 301 /en/stor/aquatic/aquarium-store/biube-pure-aquarium-35l [newsite.com...]
redirect 301 /en/store/for-the-ladies/spyorb [newsite.com...]

the redirects are organized so as the product pages precede their subcategories, and the subcateogries precede their categories.

Then at the end I added these 2 lines:
RewriteEngine on
#
RewriteRule ^(.*) [newsite.com...] [R=301,L]
redirect 301 / [newsite.com...]

g1smd

8:19 pm on Apr 17, 2010 (gmt 0)

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



Change everything to use
RewriteRule
and
[R=301,L]
in place of
Redirect
and
RedirectMatch
directives.

You can then test the domain using a preceding
RewriteCond
looking at the
HTTP_HOST
value.

waseemni

2:16 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



I've added the http host value, but how can I add multiple host tests?
I want the server to apply the rewrite conditions incase of oldsite.com, oldsite.co.uk, oldsite.eu etc...
If I write multiple rewrite conditions, it gives me a server error.

So, the file starts as follows:
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^oldwebsite\.com$

and somewhere at the end of the file I added this
Rewrite Rule ^(.*) [newsite.com...] [R=301,L]

g1smd

2:23 pm on Apr 22, 2010 (gmt 0)

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



You could use [OR] and multiple conditions, but I would try:
RewriteCond %{HTTP_HOST} [b]![/b]^(www\.newwebsite\.com)?$

waseemni

6:26 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



g1smd & jdmorgan,

thank you for your tips. strange enough, even though I added all the http_host conditions, I still get redirected with my second website which should not be redirected!

jdMorgan

12:08 am on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that a RewriteCond applies only to the first (single) rule that follows. If you have more than one rule which must be qualified by HTTP_HOST, then you will need more than one RewriteCond to qualify them...

Again, it's quite difficult to comment on code we cannot see...

And I'd like to ask everyone to please use only "example.com" as the domain/hostname in posts here...

JIm

waseemni

9:15 am on Apr 23, 2010 (gmt 0)

10+ Year Member



Sorry about that, this is the code:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR] RewriteCond %{HTTP_HOST} ^example\.nl$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.nl$ [OR] RewriteCond %{HTTP_HOST} ^example\.co.uk$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.co.uk$ [OR] RewriteCond %{HTTP_HOST} ^example\.net$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.net$ [OR] RewriteCond %{HTTP_HOST} ^example\.eu$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.eu$ [OR] RewriteCond %{HTTP_HOST} ^example\.fr$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.fr$
RewriteRule ^(.*) [newexample.com...] [R=301,L]
redirect 301 /nl/gadget-winkel/design-aquariums/aquariumwinkel/baby-biorb-15l-maanlicht [newexample.com...]
redirect 301 /nl/gadget-winkel/design-aquariums/aquariumwinkel/baby-biorb-aquarium-15l [newexample.com...]

I kept the redirect 301 because I wasn't successful in redirecting with a RewriteRule. Since the redirected urls are very specific and no pages with the same names exist on my 2nd website (which I do not want to redirect), I do not have problems in keeping it like this and it does the job done.

I think my problem is in the top part, with the rewrite rule. The moment I remove all the conditions except the first 2 conditions, then my 2nd website stops redirecting.
To make it clear, my 2nd website has a completely different url.

g1smd

11:10 am on Apr 23, 2010 (gmt 0)

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



Never mix
Redirect
and
RewriteRule
in the same
.htaccess
file. You can't guarantee what order they will be processed as
Redirect
is processed by mod_Alias and
RewriteRule
is processed by mod_Rewrite. Use
RewriteRule
for all of the rules.

When using the [OR] feature, each
RewriteCond
MUST begin on a new line.


Note too that:

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$


can be simplified to:

RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b]example\.com$



Additionally:

RewriteCond %{HTTP_HOST} ^example\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^example\.fr$


can be simplified to:

RewriteCond %{HTTP_HOST} ^example\.[b](eu|fr)[/b]$


so all of the original RewriteCond code can be reduced to just one line.

However, your code will still contain a flaw. If the requested domain name is requested with an appended port number (e.g.
http://www.example.fr[b]:80[/b]/somepage
, or similar) your code will not redirect the user.

If the site serves content at that URL and with
200 OK
status, you'll have a Duplicate Content problem on your hands.