Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond and RewriteUrl persisten arguments

         

micobros

11:17 am on Sep 30, 2008 (gmt 0)

10+ Year Member



Hello,

I'm using this :


RewriteCond %{QUERY_STRING} ^p=9$
RewriteRule ^test.php$ http://example.com/bigurl.html [R=301,L]

to redirect http://example.com/test.php?p=9 to http://example.com/bigurl.html

It works. But the problem is that "?p=9" is appended to destination URL having this for result: http://example.com/bigurl.html?p=9

How could i avoid having the argument added and the end of the URL?

I tryed this :


RewriteCond %{QUERY_STRING} ^p=9$
RewriteRule ^test.php$ - [C]
RewriteRule ^.*$ http://example.com/bigurl.html [R=301,L]

but without any success, argument is still persistent and is appended at end of URL.

Any ideas?
Thx in advance,
micobros

[edited by: jdMorgan at 3:40 pm (utc) on Sep. 30, 2008]
[edit reason] Pleas use example.com [/edit]

g1smd

12:07 pm on Sep 30, 2008 (gmt 0)

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



Add a question mark to the end of the target URL.

That will clear the query string.

You can also simplify ^.*$ to just .*

Do you want this rule to work for all URLs that have the p=9 parameter (such as /otherpage.html?p=9 for example), or just for the /test.php?p=9 one? You need to make sure you code it right.

You might not need to start anchor or end anchor the query string. It can then also redirect if extra unwanted parameters have been added to the requested URL.

jdMorgan

3:39 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, it looks like the chaining is not needed. The whole thing can be done more-efficiently with a single rule:

RewriteCond %{QUERY_STRING} ^p=9$
RewriteRule ^test\.php$ http:/example.com/bigurl.ht[b]ml?[/b] [R=301,L]

Jim

[edited by: jdMorgan at 3:39 pm (utc) on Sep. 30, 2008]

micobros

5:44 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



thanks, it works perfectly...
I read the whole doc on [httpd.apache.org...] and didn't find anything about that '?' ... Grrrr...

@g1smd: only for one specific URL
@jdMorgan: i agree with you. The test with 2 RewriteRules was to try to get rid of that persistent argument...

Thanks!

jdMorgan

6:21 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The use of "?" to clear query strings is indeed described in the documentation. But like most short and concise documentation of highly-technical things, it must be read and re-read thoroughly and slowy... :)

See the notes following the RewriteRule directive. The "really good stuff" is in the notes for each directive.

Jim

micobros

7:10 am on Oct 1, 2008 (gmt 0)

10+ Year Member



jdMorgan,

Yeah, indeed... my bad.
But,


Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string.

In think I stopped at this first sentence. Where '?' is used to actually re-inject... Which was the opposite of what i was looking for.

Another little question, in my redirects, i use special chars (ex:'é') :


RewriteCond %{QUERY_STRING} ^cat=7$
RewriteRule ^blog/$ http:/example.com/blog/term/conférence-14? [R=301,NE,L]

I do use, NE, which should not escape the special chars. But it does, and redirects me to: http://example.com/blog/term/r%E9f%E9rencement-11

Any idea on this one?

thanks,
micobros

[edited by: jdMorgan at 1:59 pm (utc) on Oct. 1, 2008]
[edit reason] Please use example.com only [/edit]

jdMorgan

2:00 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[NE] allows you to put a literal "$" or "%" in the substitution URL. It does not stop Apache from hex-encoding characters as required by RFC2396 [faqs.org].

This problem is common with sites using non-U.S. character sets. Sorry, but that's a legacy issue from the days when "the internet" was something used only by U.S. defense agencies and their academic partners.

Since I don't have any non-English sites, I have no useful experience with this problem. You might try encoding the special characters yourself using "\%2c25"-style notation in the substitution string. Also make sure your server's default character-set is correctly defined (see mod_mime). If you have a multi-lingual site, it may be helpful to change this definition depending on the <Directory> or <Location> containers used to serve each language (see Apache core).

Again, I have very limited experience with this, and it might be worthwhile to check Webmaster sites in the country whose language you are trying to use for help with these issues if no-one else replies here.

Jim

g1smd

6:18 pm on Oct 1, 2008 (gmt 0)

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



Would ditching US-ASCII or ISO-8859-1 (or whatever you use) and moving to UTF-8 solve anything?