Append query string to requested URL automagically???

Discuss practical ways rearrange URLs using mod_rewrite.

Append query string to requested URL automagically???

Postby angus15 » Thu Feb 24, 2005 3:24 pm

Hi,

When I visit my personal site, I append the query string '?ITS_ME' to the URL so I can easily weed out my visits from others when doing stats, etc. It gets tedious to append '?ITS_ME' to every request manually, so I want it to happen automagically using HTTP_REFERER.

For example, I visit the site by manually typing in http://www.mysite.com/?ITS_ME. When I click on the resume link http://www.mysite.com/resume.html, I want it to actually be rewritten as http://www.mysite.com/resume.html?ITS_ME, so it will stand out in the logs.

For the rewrite condition I have:

Code: Select all
RewriteCond %{HTTP_REFERER} (.*)ITS_ME(.*)


but I can't figure out how to get the RewriteRule to append 'ITS_ME' to the next URL that I click on without the whole URL being replaced and a 'Bad Request' error being generated.

Any ideas?

Thanks!

Bill
angus15
 
Posts: 4
Joined: Thu Feb 24, 2005 3:15 pm

Postby Caterham » Thu Feb 24, 2005 4:47 pm

but I can't figure out how to get the RewriteRule to append 'ITS_ME' to the next URL that I click on
Mod_rewrite cannot change your <a href="">-tag you click on your page to move to an other. You must change the <a href> within your Script or whatever generates the HTML-Sourcecode.

mod_rewrite acts only in the HTTP Request, not in the HTTP-Response (where the HTML source is send to the browser).
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby angus15 » Thu Feb 24, 2005 7:15 pm

Hi,

Thanks for the response Caterham. I should have clarified myself further I guess. I understand that mod_rewrite can't change the source code for the link that I click on. Besides, the source code is static HTML anyway.

What I want mod_rewrite to do is take the HTTP Request that is sent to the server by the browser and append the 'ITS_ME' to it (the request), not to actually rewrite the URLs contained in the HTML as the page is sent to the browser during the response.

If I was to write a script to do it, the pseudocode would be along the lines of:

Code: Select all
if HTTP_REFERER contains 'ITS_ME'
begin
    add 'ITS_ME' to end of QUERY_STRING
end
continue processing request...


In this way, when the request (e.g. - /resume.html) is written to the access log, it would have 'ITS_ME' appended to it (e.g. - /resume.html?ITS_ME).

Thanks again,

Bill
angus15
 
Posts: 4
Joined: Thu Feb 24, 2005 3:15 pm

Query string being appended then removed???

Postby angus15 » Fri Feb 25, 2005 7:51 am

Hi,

From mod_rewrite.log, the 'ITS_ME' is being appended, but then it's being removed. :confused: Any ideas? Here's what I've got:

Code: Select all
#Append query string to all requests from me for easy identification
RewriteCond %{HTTP_REFERER} (.*)ITS_ME(.*)
RewriteRule .* %{REQUEST_URI}?ITS_ME


Log:

Code: Select all
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (4) RewriteCond: input='http://www.mysite.com/?ITS_ME' pattern='(.*)ITS_ME(.*)' => matched
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (2) rewrite /resume.html -> /resume.html?ITS_ME  <=== THIS IS WHAT I WANT!!!
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (3) split uri=/resume.html?ITS_ME -> uri=/resume.html, args=ITS_ME
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (2) local path result: /resume.html
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (2) prefixed with document_root to /home/myhome/www_root/htdocs/resume.html
123.45.67.89 - - [25/Feb/2005:10:42:52 -0500] [mysite.com/sid#abcdef][rid#123456/initial] (1) go-ahead with /home/myhome/www_root/htdocs/resume.html [OK]


Why would it be removed again??? How do I stop it from happening?

Thanks,

Bill
angus15
 
Posts: 4
Joined: Thu Feb 24, 2005 3:15 pm

Postby Caterham » Fri Feb 25, 2005 9:55 am

No, it is not removed. It's put into the QueryString-Variable as it should be in this step of processing.

split uri=/resume.html?ITS_ME -> uri=/resume.html, args=ITS_ME


You'll get a log entry into the access log with the URL the client has originally requested, not the rewritten URL. The capturing of "the request"-variable takes place before mod_rewrite is involved. You cannot log this QueryString without sending a 302 or 301 external redirect in this standard log format. A 301/302 redirect this would result two log entries (because this are two different requests).
Last edited by Caterham on Sun Feb 27, 2005 4:44 am, edited 1 time in total.
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby angus15 » Fri Feb 25, 2005 10:01 am

Bummer... :(

I didn't realize that the logging happens before mod_rewrite is invoked. In that case, I believe what I'm trying to use mod_rewrite for is impossible.

At this point, I guess I will have to do something more drastic, like change the pages from static HTML to PHP. Unless anyone has an easier idea?

Thanks again for your reply Caterham. :D

Bill
angus15
 
Posts: 4
Joined: Thu Feb 24, 2005 3:15 pm

Postby Caterham » Sat Feb 26, 2005 4:46 am

I didn't realize that the logging happens before mod_rewrite is invoked. In that case
The logging of the request-variable takes place in an earlier stage, so you cannot manupluate it. I edited the posting above for clarification.
But this does not mean, that the log is actually written in this step of processing, only the variable is captured. The log is written in the last step of processing when all variables are collected and the response is sent to the client (browser in most cases)
In the access.log you are logging the first line of the request, which looks like

GET /posting.php?mode=reply&t=846 HTTP/1.1
.This variable will contain the initial request. It'll not change through rewriting etc. You can take a look on it with [url]web-sniffer.net[/url]

But there might be a possibility with a trick to log the QueryString which is passed actually to the file (mod_rewrite in .htaccess files works also with a trick, because the processing of the .htaccess is to late - you are already in a specific dir.)

You can try edditing your custom log format in the httpd.conf and add

Code: Select all
%U%q

U The URL path requested, not including any query string.
q The query string (prepended with a ? if a query string exists, otherwise an empty string)
. ut for this method you'll need access to your httpd.conf or vhost.conf to perform this changes.

I'm not sure if this works and if apache changes this variables in this late state of processing, but I think the variables will contain the actual query string passed to the file.
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 54 guests

cron