mod_rewrite for urls with query strings

  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

I'm totally new to mod_rewrite, so please bear with me.

I'm trying to do a 301 redirect on our really long dynamical URLs so that they point at our standard home page without the query string and don't count as duplicate content.

I've managed to get a regular expression that should work, but if I add it to the htaccess file and upload it, I just get an Internal Server Error.

This is what I've got in the htaccess:
Code: [ Select ]
RewriteEngine on
RewriteRule ^http:\/\/www\.example\.co\.uk\/index\.php\?affiliatecode=[A-Z0-9]+&campaignID=[A-Z0-9 ]+&mediumID=[A-Z0-9 ]+&activityID=[A-Z0-9 ]+$ //then the standard home page URL
  1. RewriteEngine on
  2. RewriteRule ^http:\/\/www\.example\.co\.uk\/index\.php\?affiliatecode=[A-Z0-9]+&campaignID=[A-Z0-9 ]+&mediumID=[A-Z0-9 ]+&activityID=[A-Z0-9 ]+$ //then the standard home page URL


And this is a sample url before the redirect:
Code: [ Select ]
http://homepageURL?affiliatecode=FADTHYJ&campaignID=HY789JH&mediumID=HGYFR76&activityID=7HYGF0
Replacing homepageURL with actual URL (just not allowed to post any URLs)

Thanks in advance
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Is there anyway you can see what gets recorded to your error log? That would make it really easy to figure out what is causing the problem.
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Ok, the error log says:
Code: [ Select ]
[Wed Mar 24 15:23:14 2010] [alert] [client 81.178.22.65] /home/hosted/database_name/example.xx.xx/htdocs/.htaccess: RewriteRule: bad flag delimiters


So probably something to do with flag delimiters - whatever they are!

Any ideas?
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Need a bit more information. You said you put this:

Code: [ Select ]
RewriteRule ^http:\/\/www\.example\.co\.uk\/index\.php\?affiliatecode=[A-Z0-9]+&campaignID=[A-Z0-9 ]+&mediumID=[A-Z0-9 ]+&activityID=[A-Z0-9 ]+$ //then the standard home page URL


What do you actually have after the standard home page URL which is where the flags are?

For example:

Code: [ Select ]
RewriteRule ^http:\/\/www\.example\.co\.uk\/index\.php\?affiliatecode=[A-Z0-9]+&campaignID=[A-Z0-9 ]+&mediumID=[A-Z0-9 ]+&activityID=[A-Z0-9 ]+$ http://www.somesite.com/ [QSA,L]


The [QSA,L] is the flag there. What are your flags?
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

I had [R=301,L]
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Do you have any other lines to the .htaccess file that might have bad flags? Can you paste the contents of the .htaccess file here. If you have problems posting urls due to ozzu's spam filters put parenthesis around the periods such as this:

www(.)yoursite(.)com
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Reading through your guys posts here, a few things stand out for me.

1) If you rewrite away your affiliatecode querystring variable, how are you going to track it ?

2) [pattern matching for] RewriteRule doesn't see the protocol, host, or querystring last I knew. It only sees the URI (everything between ".co.uk/" and "?variable..."). You would need to use RewriteCond with the corosponding %{VARIABLE_NAME} to work with any of them.

3) Have you read this ?

// Edit -- Clarified #2 in light of what part of RewriteRule works with the querystring
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I believe by default the RewriteRule does not see the querystring, but if you use the QSA flag it does:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Quote:
Modifying the Query String

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.


And here for 1.3 users:

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Quote:
'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.


Edit*

After re-reading what Joebert wrote I think the QSA flag can be used with the rewrite rule, but only for the rewriting part such as changing or adding new variables to the query string. I think Joebert may be right in the fact that you can't actually match the query string without using a RewriteCond first.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I should have been more specific. The first argument, or pattern matching section, of the RewriteRule directive doesn't see the querystring.

The QSA, or Query String Append flag relates to the second argument of the RewriteRule directive, or the replacement section. The flag is only really relevant when your replacement string includes a querystring itself.


Considering the following example URL. I've only included the elements you're commonly going to see in a URL.

Code: [ Select ]
protocol://domain.tld/uri?querystring=value#hash


Right off the bat, mod_rewrite never knows #hash even exists. Browsers don't send that portion to the server, ever. It was designed for navigation within the page, so it didn't make sense to implementers to have "irrelevant" information sent across the network.

mod_rewrite can see everything else in the URL, but not all of it is used when mod_rewrite tries to match a pattern against it with a RewriteRule.

Here's an example combination of mod_rewrite directives that will work with the entire example URL.

Code: [ Select ]
RewriteCond %{SERVER_PROTOCOL} ^http$
RewriteCond %{HTTP_HOST} ^domain\.tld$
RewriteCond %{QUERY_STRING} ^querystring=value$
RewriteRule ^uri$ http://domain.com [R=302,L]
  1. RewriteCond %{SERVER_PROTOCOL} ^http$
  2. RewriteCond %{HTTP_HOST} ^domain\.tld$
  3. RewriteCond %{QUERY_STRING} ^querystring=value$
  4. RewriteRule ^uri$ http://domain.com [R=302,L]
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Ok

I'm totally lost now. So even if I can get it working, I won't be able to track leads from affiliates?

The code I have in my htaccess file looks something like this (please be kind, my knowledge of mod_rewrite is primitive to say the least):

Code: [ Select ]
redirect 301 /news/wessex-milbourne-review.htm http(:)//www(.)mysite(.)co(.)uk/news/luxury-lodge-138/
redirect 301 /news/willerby-aspen-review.htm http(:)//www(.)mysite(.)co(.)uk/news/wonderful-willerby-140/
redirect 301 /news/willerby-boston-lodge-review.htm http(:)//www(.)mysite(.)co(.)uk/willerby-boston-holiday-home-142/
redirect 301 /news/willerby-granada-xl-holiday-home-review.php http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-granada-xl-holiday-home-144/
redirect 301 /news/willerby-leven-review.htm http(:)//www(.)mysite(.)co(.)uk/news/willerby-leven-146/
redirect 301 /news/willerby-rio-review.htm http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-two-bedroom-rio-holiday-home-148/
redirect 301 /news/willerby-signature-review.htm http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-signature-holiday-home-150/
redirect 301 /news/winterization-article.htm http(:)//www(.)mysite(.)co(.)uk/news/getting-ready-for-winter-152/
redirect 301 /login/index.php http(:)//www(.)mysite(.)co(.)uk/index.aspx?op=login&partner=leisuredays&product=holidayhome
redirect 301 /longbeach/index.php http(:)//www(.)mysite(.)co(.)uk/index.php?affiliatecode=LONGBE+LP&campaignID=OLARK546&mediumID=ONARK621&activityID=NTNET821



ErrorDocument 404 /404.php


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>
  1. redirect 301 /news/wessex-milbourne-review.htm http(:)//www(.)mysite(.)co(.)uk/news/luxury-lodge-138/
  2. redirect 301 /news/willerby-aspen-review.htm http(:)//www(.)mysite(.)co(.)uk/news/wonderful-willerby-140/
  3. redirect 301 /news/willerby-boston-lodge-review.htm http(:)//www(.)mysite(.)co(.)uk/willerby-boston-holiday-home-142/
  4. redirect 301 /news/willerby-granada-xl-holiday-home-review.php http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-granada-xl-holiday-home-144/
  5. redirect 301 /news/willerby-leven-review.htm http(:)//www(.)mysite(.)co(.)uk/news/willerby-leven-146/
  6. redirect 301 /news/willerby-rio-review.htm http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-two-bedroom-rio-holiday-home-148/
  7. redirect 301 /news/willerby-signature-review.htm http(:)//www(.)mysite(.)co(.)uk/news/the-willerby-signature-holiday-home-150/
  8. redirect 301 /news/winterization-article.htm http(:)//www(.)mysite(.)co(.)uk/news/getting-ready-for-winter-152/
  9. redirect 301 /login/index.php http(:)//www(.)mysite(.)co(.)uk/index.aspx?op=login&partner=leisuredays&product=holidayhome
  10. redirect 301 /longbeach/index.php http(:)//www(.)mysite(.)co(.)uk/index.php?affiliatecode=LONGBE+LP&campaignID=OLARK546&mediumID=ONARK621&activityID=NTNET821
  11. ErrorDocument 404 /404.php
  12. <IfModule mod_rewrite.c>
  13. RewriteEngine On
  14. RewriteBase /news/
  15. RewriteCond %{REQUEST_FILENAME} !-f
  16. RewriteCond %{REQUEST_FILENAME} !-d
  17. RewriteRule . /news/index.php [L]
  18. </IfModule>


The last 301 is an example of a affiliate (longbeach) whose URL redirects to the tracking URL, but really I want this to go to our homepage, but still track it. I was thinking I'd need to have 2 301s for each affiliate, one redirecting a nice URL to the tracking URL with query string, and one directing this to the normal homepage of our site. I'm not even sure if it's possible.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

You can think of mod_rewrite as a way to alter the address your user is asking for before your server is asked for it. It's a lot like standing right behind the user while they type an address and correcting them if they type "sutff" instead of "stuff".

Now technically, the request has gotten to the server when mod_rewrite works with it, so really it's more like standing behind someone at a night club and correcting them if they begin to say their nickname instead of the name the bouncer will actually find on the list.

All mod_rewrite does is handle aliases. Any querystring variables that aren't included in the result URL are thrown away, the server never knows they existed.

In our bouncer scenario this would be like coming to the door and before saying their name is "slick rick" you correct them to "rick" and the bouncer checks the list for "rick". After rick gets inside the door he is from there on reffered to as "rick" and not "slick rick" because the bouncer gave him a name tag with just plain "rick" on it. Rick has also miraculously forgotten he was once referred to as "slick rick".

--

What you'll need to do is setup your 301 redirects for your affiliates to a logging script with their affiliate id attached. Within that script you will handle your affiliate logging, and place the affiliate id in a session and send a session cookie along with a second 301 redirect request from within the script.

--

So basically, you're not going to use mod_rewrites RewriteRule or RewriteCond at all to accomplish what you need to do.
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Thanks, Joebert and Bigwebmaster, I'll post a request for help in the programming forum when I get my head around it!

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 7 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.