Redirect all initial requests

New to mod_rewrite? This is a good place to start.

Postby alhermette » Tue Mar 31, 2009 1:22 am

Ah yes - I hadn't realised that it would create a loop but I see why now.

I'm not sure that using php to output files will work in my circumstances.

Yes I have access to httpd.conf but this is not something that I have touched before, what's your idea?

I am thinking that there must be some other way that I can transmit information from the tracking script to .htaccess simply. Would it be possible for instance to set some form of HTTP header information? I am thinking that I could "borrow" something like HTTP FORWARDED FOR or similar to pass information in a form that mod-rewrite can access. This is not something that I have researched, just bouncing ideas around out load at the moment.

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Tue Mar 31, 2009 8:34 am

Yes I have access to httpd.conf but this is not something that I have touched before, what's your idea?

You could use a RewriteMap to run the tracking script. This would mean that the request would not need to be redirected.

In your <VirtualHost>
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap tracking prg:/path/to/php/tracking/script

RewriteRule \.(txt|gif|jpe?g|png|css|ico|xml|xsl|pdf)$ - [L]

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/.*)?$ [NC]
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?TRACKING=DONE(;\ .*)?$ [NC]
RewriteCond %{QUERY_STRING} !^(.*&)?TRID=1(&.*)?$ [NC]
RewriteCond ${tracking:$1} .*
RewriteRule ^(.*)$ - [NC,QSA,CO=TRACKING:DONE:.example.com,PT,L]

Your script will need modification (shebang line and no redirection).

Would it be possible for instance to set some form of HTTP header information? I am thinking that I could "borrow" something like HTTP FORWARDED FOR or similar to pass information in a form that mod-rewrite can access. This is not something that I have researched, just bouncing ideas around out load at the moment.

You can set response headers but it is unlikely the browser would return them on future requests.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Wed Apr 01, 2009 4:54 am

OK that went right over my head - I'll have to go do some research on rewrite maps and come back.

Your help is much appreciated,

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby alhermette » Mon Jun 01, 2009 6:12 am

OK I've done what research I can and can't find anything that demonstrates how a php rewrite map should be written.

I think I understand what the shebang line should be but seeing a relevant example would be reassuring. The actual php part is no problem except that in the original file my output was a redirect to the newly constructed url. With a rewrite map I will still have to output a url somehow and I haven't been able to find out how that should be done.

For instance let's say the last line of my code is:

$URL_to_rewrite_to = 'http://www.domain.com/directory/page.html';

How do I tell httpd.conf that the url I want it to rewrite to is contained in the variable $URL_to_rewrite_to (and do I need the http://www.domain.com or can that be omitted).

If anyone can point me in the right direction I would be most grateful.

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Tue Jun 02, 2009 10:35 am

Your script has to work from the command line.

With a rewrite map I will still have to output a url somehow and I haven't been able to find out how that should be done.

For instance let's say the last line of my code is:

$URL_to_rewrite_to = 'http://www.domain.com/directory/page.html';

How do I tell httpd.conf that the url I want it to rewrite to is contained in the variable $URL_to_rewrite_to (and do I need the http://www.domain.com or can that be omitted).

You don't need to do anything at the end of the script. Just log the request and end the script. The URL should be in STDIN.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Thu Jun 04, 2009 9:27 am

Is this along the right lines?

#!/usr/bin/php
<?php

$stdin = fopen('php://stdin', 'r');
$stdout = fopen('php://stdout','w');

while( $url = trim(fgets($stdin)))
{
// Put my php processing script here that modifies $url
fwrite($stdout, $url.".html\r\n");
}
?>


What exactly will appear in stdin? will it include the domain name? http://? the query string?

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Thu Jun 04, 2009 1:38 pm

Is this along the right lines?

I've never done it. You might be better off asking on a PHP forum about running a script form the command line.

Code: Select all
fwrite($stdout, $url.".html\r\n");

You do not need to return anything. You don't need STDOUT.

What exactly will appear in stdin?

Code: Select all
RewriteCond ${tracking:$1} .*

At the moment $1, so it would be "dir/file.ext" from http://www.example.com/dir/file.ext?abc=def. You could pass other things like HTTP_HOST and the QUERY_STIRNG
Code: Select all
RewriteCond ${tracking:%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}} .*

would get you "www.example.com/dir/file.ext?abc=def". (It will always include a ? even for an empty query string.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Fri Aug 14, 2009 3:44 pm

Options +FollowSymLinks

RewriteEngine On

RewriteRule \.(txt|gif|jpe?g|png|css|ico|xml|xsl|pdf)$ - [L]

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/.*)?$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/.*)?$ [NC]
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?Tr=OK(;\ .*)?$ [NC]
RewriteCond %{QUERY_STRING} !^(.*&)?TRID=1(&.*)?$ [NC]
RewriteRule ^(.*)$ /Tracking/index.php?Path=$1 [NC,QSA,L]


I am using the code above in my root .htaccess to rewrite all initial requests to a tracking script quite sucessfully. I have a directory which has its own .htaccess file though and this is causing me some issues as I need traffic going to this folder to go to the tracking script as well.

I understand that I may be able to use "RewriteOptions inherit" but that in this case the subdirectory's htaccess rules will be processed before the root level htaccess. Due to the structure of the htaccess in the subdirectory this is not what I want. I specifically need the root level htaccess to be processed (which should rewrite to tracking.php and this in turn will redirect back to the subdirectory with a modified url) before the htaccess in the subdirectory is processed.

Can I simply copy the code from the root htaccess into that of the subdirectory and then use the [S] flag to skip the existing rules in there if my tracking.php rewrite rule is matched? If so I am not sure quite how to do this, can anyone help.

Thanks,

Allan
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Sun Aug 16, 2009 6:55 am

You don't need
Code: Select all
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/.*)?$

(it's the same as the third condition).

Try adding
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule \.(txt|gif|jpe?g|png|css|ico|xml|xsl|pdf)$ - [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com(/.*)?$ [NC]
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?Tr=OK(;\ .*)?$ [NC]
RewriteCond %{QUERY_STRING} !^(.*&)?TRID=1(&.*)?$ [NC]
RewriteRule ^(.*)$ /Tracking/index.php?Path=directory/$1 [NC,QSA,L]

to the beginning of your /directory/.htaccess. I'm not sure if you want the first rule or not. If you want it to only apply to the tracking rule, you could use this condition instead
Code: Select all
RewriteCond %{REQUEST_URI} !\.(txt|gif|jpe?g|png|css|ico|xml|xsl|pdf)$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Tue Aug 18, 2009 2:51 am

OK thanks for that, I didn't quite understand how the [L] flag worked but I see now.

I have realised that there are some subfolders in the directory that I am dealing with that I do not want to rewrite to the tracking script at all so I need to put in a condition that does not rewrite requests to:

/directory/sub1
/directory/sub2
/directory/sub3

Not quite sure on the correct syntax for doing that.

Also I use domain aliases so is it possible to use something like the following in place of example.com:

RewriteCond %{HTTP_REFERER} !^%{HTTP_HOST}(/.*)?$ [NC]


Finally is the rewrite rule:

RewriteRule ^(.*)$ /Tracking/index.php?Path=directory/$1 [NC,QSA,L]


still root relative or is it realtive to the directory where the .htaccess file is residing?
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

PreviousNext

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron