Forum Moderators: phranque

Message Too Old, No Replies

Rewriterule for the external links

         

mdesign

6:31 pm on May 26, 2007 (gmt 0)

10+ Year Member



Is it possible set this rule via .htaccess?

if url-1 !mydomain
redirect url-1 to [mysite.com...]

I've tried this, but the RewriteConds don't work

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^http://www\.mysite\.com [NC]
RewriteRule ^(.*) [mysite.eu...] [R=301,L]

The result I want is: [mysite.eu...]

Any idea?

g1smd

7:24 pm on May 26, 2007 (gmt 0)

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



RewriteCond %{HTTP_HOST} !^http://www\.mysite\.com [NC]

should be:

RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com [NC]

Lose the http:// and maybe add the brackets so that it works for both www and non-www requests.

jdMorgan

7:37 pm on May 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be aware the that hostname is not part of the URL-path seen by RewriteRule.

Also, there is no apparent need to do an external redirect: An internal rewrite to the script should suffice, and then the script can do its external redirect.


RewriteCond %{HTTP_HOST} !^(www\.)?example\.eu [NC]
RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$
RewriteRule (.*) /redirect/index.php?option=http://%1/$1 [L]

Here we build the "option=" parameter in the form you specified by appending the back-referenced hostname and URL-path to "http://"

The second subpattern in the second RewriteRule allows (but does not require) a port number appended to the hostname.

Jim

mdesign

10:37 pm on May 26, 2007 (gmt 0)

10+ Year Member



Thank both of you for the answer.

RewriteCond %{HTTP_HOST}!^(www\.)?example\.eu [NC]
RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$
RewriteRule (.*) /redirect/index.php?option=http://%1/$1 [L]

It has no effect. If I click an external link the browser open that URL immediately. And I did several other attempts.

Perhaps I cannot obtain that I would using htaccess :(

jdMorgan

12:54 am on May 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have other working rules?

If not, you will need to enable mod_rewrite:


Options +FollowSymLinks
RewriteEngine on

Jim

mdesign

9:19 am on May 27, 2007 (gmt 0)

10+ Year Member



Jim,
mod_rewrite is enabled.
This is my root htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.eu [NC]
RewriteCond %{HTTP_HOST} ^([^:]+)(:[0-9]+)?$
RewriteRule (.*) /redirect/index.php?option=http://%1/$1 [L]

This .html file is in the root:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<a href="http://www.google.com">link</a>

</body>
</html>

When I click on the link, the browser opens the Google homepage.

The code seems to be correct and in the past I've already used the rewriterules on the same server...

?

Mario

jdMorgan

2:50 pm on May 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the link goes to google.com, the client browser will never send that request to *your* server. It will send the request to google.com, so your rules will have no effect.

I assume you're trying to do exit-click-tracking, and if so, you will have to change the link on your page to point to your own server, like <a href="http://www.example.com/exit-tracker/http://www.google.com">Go to Google</a> and then write a rule to handle that.

Alternately, you can use client-side JavaScript to load a 1x1 transparent image from your server whenever an outbound link on your page is clicked, and track the outgoing click based on that. Google uses this exact method [webmasterworld.com] periodically to track clicks on the top three or four search results listings.

Jim

mdesign

6:42 pm on May 27, 2007 (gmt 0)

10+ Year Member



If the link goes to google.com, the client browser will never send that request to *your* server. It will send the request to google.com, so your rules will have no effect.

LOL :)
Ok.. I will use an alternate way :P

Tnx.