Forum Moderators: phranque

Message Too Old, No Replies

Delete all after? sign using htaccess?

Deleting all on a URL after a sign

         

Shadowcat

2:15 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Hi everybody, I have a site up at the moment.. and another site which sends me LOTS of traffic when I get listed on it insists on adding?&referrer=http://www.example.com (obviously not that site...). Anyway, I use Mambo with the SEO module on... that bit at the end of the URL messes it up and so it presents the viewer with 'You are not authorised to view this resource'.

I saw this thread : [webmasterworld.com...]

About using htaccess to delete all after the % sign.. I tried adapting this but could not manage it... however I know from testing that a similar solution would work.. it just needs to delete all after the? sign... so the URL requested would be:

http://www.example.com/content/1/2/?&referrer=http://www.example.com

but the URL presented would be http://www.example.com/content/1/2

Hope you guys know what I'm talking about.. this is driving me mad :) ( And I'm not that great with .htaccess)

I would really appreciate some help! Thanks!

[edited by: jdMorgan at 2:45 pm (utc) on Aug. 4, 2005]
[edit reason] Examplified. [/edit]

jdMorgan

2:59 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shadowcat,

Welcome to WebmasterWorld!

The key here is that the query string (everything following the "?") is not part of a URL, but rather, data appended to the URL to be passed to the resource at that URL. For this reason, it is handled separately by mod_rewrite.

You can clear the query string by appending a "?" to the substitution URL.

In order to avoid an 'infinite' rewrite loop, you should also test the query string to be sure it is non-blank or contains a specific value before rewriting. Otherwise, you'll get a loop.

A simple example would be to rewrite /foo.php?prod=bar to /foo.php:


RewriteCond %{QUERY_STRING} ^prod=bar$
RewriteRule ^foo\.php$ /foo.php? [L]

Hopefully, that will get you going. See the reference documents cited in our forum charter [webmasterworld.com] for more information.

Jim

Shadowcat

3:58 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Hi, thanks for the reply.

Using your code sample and changing it for my site.. I can't get it to do anything.. I changed the prod=bar to the referrer code and the foo.php to the index.php .. however it does not delete anything.

When I tried the code in the thread I mentioned in my first post.. it would delete the URL if I replaced the? with a % in the referrer code which this side appends.. is it not possible to just use modified code for? instead of %?

I am very new to htaccess.. and it is kinda daunting O_o

jdMorgan

5:34 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any mod_rewrite code working?

If not try:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^foo\.html$ /index.php [L]

Request foo.html with your browser, and you should see the contents of index.php. Then report your results.

Since you haven't posted your code, we can't really discuss it.

Jim

Shadowcat

1:03 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*) index.php

That is my current .htaccess so maybe it is interfering with the other code... I must have that code though as it is essential for Mambo (my CMS) to work for search engines..

Things do work though when I tried the code I mentioned in the other thread with the % sign... deleted it right away... just need to figure out a system to delete after? .. I no so little about .htaccess and am finding it quite difficult to interpret the commands..

jdMorgan

5:02 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, the rule order is important. But you need to decide if the URLs subject to the first rule should also be rewritten by the second rule, and whether you want to do an internal rewrite (which should suffice to correct the problem with mambo), or an external 301 redirect, which would correct this link in search engine indexes if they spider your link on the other site.

The following assumes that you want an internal rewrite, and that the second ruleset also applies to the URLs rewritten by the first ruleset.


RewriteCond %{QUERY_STRING} referrer=http://www\.example\.com
RewriteRule ^content/1/2$ /content/1/2?
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]

Jim

Shadowcat

5:26 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Thanks.. this is helping to unfog things in my mind :)

I would want things which have been rewritten by the first ruleset to be subject to the second as without that the content would not be fetched correctly.

As the site links to different types of content and the (for example):

"content/1/2"

etc changes with each new piece of content, how would I go about accounting for that?

jdMorgan

5:32 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that depends on how much the requested URL-path changes, and whether you want to remove that query from *all* requests or just a pre-defined few, and whether that query might be combined with query info that you need to keep.

It is best to avoid applying rewrites to all URL-paths, since performance is affected and some very unpleasant side-effects can occur unless it's well thought-out.

The coding is usually easy -- it's defining the problem that's difficult, as this thread demonstrates.

So what, precisely, are the conditions under which you want to remove that query?

Jim

Shadowcat

5:56 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Hm, I can see your point about not applying it to all url paths.. the thing is.. it could be any URL path but it will always be from a specific referrer.

Basically I (or someone else) submits my link to them and then they list my site.. but their script shoves?&referrer=http://www.example.com on the end of my URL. That bit never changes... Just some code to recognise the referring URL, then chop the '?&referrer=http://www.example.com' off the end would be perfect, if it is coming from 1 refering URL.. and it can be recognised.. hopefully it would make things faster?

They could link to any part of the site though and the URL would change significantly... but they will always stick the '?&referrer=http://www.example.com' at the end...

jdMorgan

6:49 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, then. To accept *any* requested URL with that query string, change the rule to

RewriteRule (.*) /$1?

Jim