Forum Moderators: phranque

Message Too Old, No Replies

Url rewrite, remove one variable from url

         

casafree

2:24 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



Hello all,
i would like to do something like this but it doesn't work :

RewriteEngine On
RewriteRule viewtopic.php?topic_id=(.*)&forum=(.*) viewtopic.php?topic_id=$1 [R=301,L]

i want the url to look like : website.com/viewtopic.php?topic_id=5
instead of website.com/viewtopic.php?topic_id=5&forum=2

Anybody have an idea about how to do that ?

Greetings from Morocco.

Caterham

2:59 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



[httpd.apache.org ]:
What is matched?

The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

casafree

3:12 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



I Know so few about rewriting ...
i tried this :

RewriteEngine On
RewriteCond %{QUERY_STRING} topic_id=(.*)&forum=(.*)
RewriteRule viewtopic.php [website.com...] [R=301,L]

didn't work neither ...

jdMorgan

4:20 pm on Feb 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use %1 to refer to the first parenthesized sub-pattern match in the preceding RewriteCond, as documented.

RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^topic_id=([^&]*)&forum=([^&]*)
RewriteRule ^viewtopic\.php$ http://example.com/viewtopic.php?topic_i[b]d=%1[/b] [R=301,L]

Jim

g1smd

7:48 pm on Feb 27, 2009 (gmt 0)

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



Make sure you also cater for when someone asks for a URL like this:

website.com/viewtopic.php?forum=2&topic_id=5

casafree

8:21 pm on Feb 27, 2009 (gmt 0)

10+ Year Member



jdMorgan, it worked like a charm ! Thank you very very much !
G1smd, my 5 years old url are all formated that way "topic_id - forum", but how can i make it work the way you said ?
Because actualy, when i try this url viewtopic.php?forum=2&topic_id=5 it doesn't work.