Forum Moderators: phranque

Message Too Old, No Replies

.htaccess RewriteCond

Particular URL to another directory

         

Alternative Future

10:33 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I have a problem with my .htaccess and appreciate all help on offer :)

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteRule ^(.*)$ /dir/$1 [L]
It then goes on to do some other Conditions and Rules for other domains I have, which works ok, its just the above part which seems to be causing the server to go into an infinite loop.
What I am trying to do is check the incoming HTTP_HOST for domain.net and send it to the index.jsp I have in the dir (directory)

If I do the above like this is works:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteRule ^(.*)$ http://www.anotherdomain.com [L]

But my problem is getting it to go to the directory rather than another domain!

Any ideas on where I am going wrong?

Once again thanks to all,

-gs

[edited by: jdMorgan at 2:26 am (utc) on Nov. 24, 2003]
[edit reason] De-linked URL [/edit]

claus

10:47 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's looping because you redirect to the same host that you are checking for.

You will want a rule that only redirects to /dir/ if the user is not already in /dir/. Try this:

Options +FollowSymlinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /dir/$1 [L]

I think this will do the trick

/claus

Alternative Future

10:52 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi claus,

Many thanks that did the trick, stopped the loop and redirected to the directory.

Is there a way I can continue to show the request domain in the address bar i.e. http://www.domain.net/dir/?

Thanks again

-gs

[edited by: jdMorgan at 2:26 am (utc) on Nov. 24, 2003]

claus

11:00 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



- it doesn't? Try changing the last line to:

RewriteRule ^(.*)$ http://www.domain.net/dir/$1 [R,L]

Hope this does it then ;)
/claus

[edited by: jdMorgan at 2:27 am (utc) on Nov. 24, 2003]
[edit reason] De-linked [/edit]

Alternative Future

11:05 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I also thought that and put it to test, the result was the same it resolves to my main domain anotherdomain.net!

Would it help to see the rest of the .htaccess?

My understanding though is that the [L] exists mod_rewrite.

I cannot thank you enough for your help.

-gs

claus

11:40 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right about the L flag, so it's probably not because of the rest of the .htaccess.

It could be your server configuration that has an opposite rule, redirecting domain.net to anotherdomain.net - or it can be your DNS settings that are not set up as a true alias - rather some script redirects to the main domain. I've seen some registrars/hosts do this.

If you type in www.domain.net/dir/somefile.htm in your browser, what happens? What does the Server Header Checker [webmasterworld.com] say?

/claus

Alternative Future

11:53 pm on Nov 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Getting a 301 moved permanently:
Input URL: http://www.domain.net/dir/
Effective URL: http://www.anotherdomain.net/dir/

HTTP/1.1 301 Moved Permanently
Date: Sat, 22 Nov 2003 23:51:03 GMT
Server: Apache/1.3.20 Sun CQhost (Unix) Chili!Soft-ASP/3.6.2 PHP/4.3.3 mod_ssl/2.8.4 OpenSSL/0.9.6b Resin/2.1.10 mod_auth_pam_external/0.1 FrontPage/4.0.4.3 mod_perl/1.25
Location: http://www.anotherdomain.net/dir/
Connection: close
Content-Type: text/html; charset=iso-8859-1

This give us any clues to a server configuration issue?

KR,

-gs

[edited by: jdMorgan at 2:28 am (utc) on Nov. 24, 2003]

claus

12:03 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It confirms that you have two rules running in circle. To zero in on where the unwanted rule is, try an IP lookup using a DNS tool to see if the IP's for the two domains are the same.

If they are the same, the rule is probably found in your server configuration (it could still be your host though), and if it's not, it's probably your registrar/host.

Do you recall what process you used to get the "domain.net" domain redirected to your server in the first place?

/claus


Edit: I wrote "whois tool" that was a mistake, i must have been absent minded. It should have been a "DNS lookup" tool - Google finds 388,000 of them at the moment if you don't have a favorite already (not recommending one due to linking policies of this board) ;)

[edited by: claus at 12:13 am (utc) on Nov. 23, 2003]

Alternative Future

12:11 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The process to get the main domain redirected to my server was out of my hands and the responsibility of the host of my server. As the domain is registered with another company and parked with the server hosts.

What’s the best IP lookup tool to use any recommendations?

Thanks again,

-george

claus

12:25 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, i think it's the hosts, then. It could sound like that. They have probably made a forward script in stead of pointing the domain to the same (virtual) server as a server alias.

If that's the case, you could ask them to implement the redirecting rule to /dir/$1 in stead, then you'll not need it in your .htaccess. Hosts have very different policies for forwarding domains, so i don't know how much they will be able to help you without charging a bit of money.

As for "DNS tools", i can only mention a searchphrase on Google, as i had the link to my favorite removed in another post once (i'm not affiliated with any of these), so i guess it's against policy somehow. Anyway, they all do the same and there's lots of them, just pick the first. (That can hardly be considered a recommendation during a Google update, as serps tend to shift)

/claus

Alternative Future

12:34 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have had other domains running hence the reason I have other rules in the .htaccess i.e. when I used to put domain.net into the address bar it was shown, and Inktomi crawled that domain for ages reason why I have the other rules in the .htaccess.

Hmm it taking me a while to think things out here ;) perhaps I should go to bed and start afresh in the morning, if I can think of any more details then I shall update this thread.

But in the meantime many many thanks claus your help has really helped me out so far (at least I have it redirecting to the directory) tomorrow I shall aim for the domain also :)

g'night,

-george

claus

1:30 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> and Inktomi crawled that domain for ages reason why I have the other rules in the .htaccess.

That sounds odd. I'm a bit confused now. So, domain.net does not point to anotherdomain.net? Perhaps you should post all .htaccess lines that has to do with domain.net after all, i might have misunderstood the situation. Anyway, it's 2:30 AM here, so that will have to wait.

/claus

Alternative Future

11:19 am on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Morning Claus and everyone,

Here is the full .htaccess


Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteCond %{REQUEST_URI} !^/domain/
RewriteRule ^(.*)$ http://www.domain.net/dir/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.net
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*\ \(Slurp/
RewriteRule ^robots\.txt$ /alternate_robots.txt [L]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.net
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteRule ^(.*)$ http://www.anotherdomain.net/$1 [R=permanent,L]

As you can see the first three lines (not including the standard two) is what I/we are working on. Then after the first [L] which exits the mod_rewrite I have a condition and rule for Inktomi (this is where Ink picked up on domain.net in the past) again this exits mod_rewrite after the condition then we go onto the condition that checks that the requesting URL isn't anything apart from my main domain (anotherdomain) this does the permanent redirect and exits mod_rewrite.

I hope I am not confusing matters worse :)

There is no .htaccess in the dir (directory) to make it resolve to anotherdomain either...

Thx,

-george

[edited by: jdMorgan at 2:31 am (utc) on Nov. 24, 2003]

claus

1:39 pm on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have three parts:

---------------------------------------------------

# part 1
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteCond %{REQUEST_URI} !^/domain/
RewriteRule ^(.*)$ http://www.domain.net/domain/$1 [R=301,L]

# part 2
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.net
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*\ \(Slurp/
RewriteRule ^robots\.txt$ /alternate_robots.txt [L]

# part 3
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.net
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteRule ^(.*)$ http://www.anotherdomain.net/$1 [R=301,L]

---------------------------------------------------

I have edited the RewiteRule in part 1, as i assumed a typo - that the name of "/dir/" should be the same as the name of "domain". Also, i've made part 1 redirect permanent, like part 3 does.

I see no problems here, as part 1 and part 3 should not interfere with each other. If you want to make absolutely sure, you could include the test for "domain.net" from rule 1 in rule 3, like this:

---------------------------------------------------

# part 3, expanded
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.net
RewriteCond %{HTTP_HOST} !^www\.domain\.net
RewriteRule ^(.*)$ http://www.anotherdomain.net/$1 [R=301,L]

---------------------------------------------------

This will make sure that "domain.net" is not included in RewriteRule 3, so you have:

- rule 1 for "domain.net"
- rule 2 for Slurp, robots.txt
- rule 3 for all that is not "domain.net", "anotherdomain.net" or your IP.

/claus

[edited by: jdMorgan at 2:33 am (utc) on Nov. 24, 2003]
[edit reason] De-linked [/edit]

Alternative Future

2:02 pm on Nov 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fantastic!

Nice one Claus, I added the condition in rule 3 to exclude domain.net from that rule... and it works...

I am familer with all the rules now in my .htaccess apart from:
#This checks the requesting URI is not the domain directory?
RewriteCond %{REQUEST_URI}!^/domain/
Does this condition check that we are not already in the directory named after the domain i.e. where the RewriteRule goes to in rule 1?

Edit Sorry you have already explained this rule I have it now!

Many thanks for that. :)

-george

Edited by Alternative_Future