Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule - changing the path based on a parameter

Rewrite rule - changing the path based on a parameter

         

ibizavenue

9:16 pm on Jan 28, 2007 (gmt 0)

10+ Year Member



Hello,

I'm having a little trouble finishing this and am hoping that someone can help. Here's what I need to do in the .htaccess file

1. http://www.example.com/fre/abc.php?setlang=eng
--> http://www.example.com/abc.php?setlang=eng

Here's what I have for the above and seems to be working fine

RewriteCond %{QUERY_STRING} ^setlang=eng
RewriteRule ^fre/(.*)$ http://www.example.com/$1?setlang=eng [L]

2. I now need to handle additional parameters either before or after the setlang=eng parameter (there can also be both parameters before and after)

For example:
http://www.example.com/fre/abc.php?setlang=eng&id=1
--> http://www.example.com/abc.php?setlang=eng&id=1

OR

http://www.example.com/fre/abc.php?id=1&setlang=eng
--> http://www.example.com/abc.php?id=1&setlang=eng

OR

http://www.example.com/fre/abc.php?id=1&setlang=eng&print=1
--> http://www.example.com/abc.php?id=1&setlang=eng&print=1

Another way of saying this is if the path includes the "fre" and the parameter list includes "setlang=eng" I need to get rid of "fre" in the path but leave the parameters as is.

Thanks.

jdMorgan

9:41 pm on Jan 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand your question, the only change necessary is to make the query string pattern accept additional parameters, and to remove the "replace query string" function in the rule:

RewriteCond %{QUERY_STRING} &?setlang=eng&?
RewriteRule ^fre/(.*)$ http://www.example.com/$1 [L]

With this configuration, the query string is passed through the RewriteRule unchanged, while the RewriteCond pattern is unanchored, and will accept preceding or trailing parameters while at the same time rejecting other possible name/value pairs like "oldsetlang=english" because "setlang=eng" must be preceded and/or followed by either nothing or by an ampersand.

In other words, the query string pattern says that if any characters precede or follow "setlang=eng", then the characters directly adjacent to setlang=eng must be ampersands.

Jim

[edited by: jdMorgan at 9:41 pm (utc) on Jan. 28, 2007]

ibizavenue

10:56 am on Jan 29, 2007 (gmt 0)

10+ Year Member



Thanks Jim, you understood correctly and your suggestion works great.

1. I'm now onto the next part. If the path includes fre and there are parameters (except of course when I have the parameter setlang=eng, which I'm handling by placing the one you wrote first in the .htaccess) I'd like to append
&setlang=fre
and strip out the fre from the path.

Here's where I'm at:

RewriteCond %{QUERY_STRING} .
RewriteRule ^fre/(.*)$ http://www.example.com/$1&setlang=fre [L]

Unfortunately the result of this is:
http://www.example.com/abc.php?logout=1
--> http://www.example.com/abc.php&setlang=fre?logout=1

Which isn't right. I want
--> http://www.example.com/abc.php?logout=1&setlang=fre

2. Maybe I should take a step back to explain what I'm trying to do globally.

If a URl contains /fre/ as part of the path, then I need to always strip it out and add the parameter
setlang=fre

The exception is when I have /fre/ as part of the path and the parameter
setlang=eng
In this case I still want to strip out the fre but not add a setlang=fre

So far I'm trying to do this by the following:

# handle the exception
RewriteCond %{QUERY_STRING} &?setlang=eng&?
RewriteRule ^fre/(.*)$ http://www.example.com/$1 [L]

# handle the case with parameters (NOT WORKING YET)
RewriteCond %{QUERY_STRING} .
RewriteRule ^fre/(.*)$ http://www.example.com/$1&setlang=fre [L]

#handle the case with no parameters
RewriteRule ^fre/(.*)/?$ http://www.example.com/$1?setlang=fre [L]

is there a better and simpler way to achieve what I'd like to do. My solution requires that the above stay in this order. Is there a way to do this where order doesn't matter?

Thanks.

jdMorgan

5:58 pm on Jan 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The exception is when I have /fre/ as part of the path and the parameter
setlang=eng. In this case I still want to strip out the fre but not add a setlang=fre

 # If setlang=eng query, remove /fre from URL-path, retaining original query
RewriteCond %{QUERY_STRING} &?setlang=eng&?
RewriteRule ^fre/(.*)$ http://www.example.com/$1 [L]
#
# If not setlang=eng query, remove /fre from URL-path, append setlang=fre to original query
RewriteCond %{QUERY_STRING} !&?setlang=eng&?
RewriteRule ^fre/(.*)$ http://www.example.com/$1?setlang=fre [QSA,L]

Jim