Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite or RedirectMatch?

mod_rewrite or RedirectMatch

         

prof_ethan

12:48 am on Jun 18, 2009 (gmt 0)

10+ Year Member



Hello people and JDmorgan, im a follower of the forum, but so far until today i faced a problem and need your help.

the scenario, i can only use apache.conf, not htaccess:

1) http://example.com/yy/zz/request?request_type=default&country=us
to
[newurl.com...]

2) http://example.com/yy/zz/request?request_type=default&country=japan
to
[newurl.com...]

3) http://example.com/yy/zz/request?request_type=default&country=taiwan
to:
[newurl.com...]

so far, ive tried redirectmatch but couldnt make it.
The closest ive get was with this line:
RewriteRule ^/yy/zz/request\?(.)request_type(.*)$ [newurl.com...]

but for this, im getting this type of redirection: [newurl.com...]

this is not good, im seeing that i have 2 variables, but are separated by the symbol & and couldnīt match it with regular expresions.

Also, i donīt know if i will be any aditional rule.

Can you give me a little piece of your expertise to work it out?

Thanks a lot!

[edited by: jdMorgan at 12:25 am (utc) on June 19, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:09 am on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to handle each case separately, as mod_rewrite has no built-in facility for associating country names with country codes:

RewriteCond %{QUERY_STRING} ^request_type=default&country=us$
RewriteRule ^/yy/zz/request$ http://www.example.com/us/? [R=301,L]
#
RewriteCond %{QUERY_STRING} ^request_type=default&country=japan$
RewriteRule ^/yy/zz/request$ http://www.example.com/jp/? [R=301,L]
#
RewriteCond %{QUERY_STRING} ^request_type=default&country=taiwan$
RewriteRule ^/yy/zz/request$ http://www.example.com/tw/? [R=301,L]

An alternative and more efficient (but more complicated) method is to kludge up an association-table-like stack of RewriteConds:

RewriteCond %{QUERY_STRING} ^request_type=default&country=([a-z]+)$
RewriteCond %1>us ^us>([a-z]+)$ [OR]
RewriteCond %1>jp ^japan>([a-z]+)$ [OR]
RewriteCond %1>tw ^taiwan>([a-z]+)$ [OR]
RewriteCond %1>cn ^china>([a-z]+)$ [OR]
RewriteCond %1>vn ^vietnam>([a-z]+)$ [OR]
RewriteCond %1>sg ^singapore>([a-z]+)$ [OR]
RewriteCond %1>th ^thailand>([a-z]+)$
RewriteRule ^/yy/zz/request$ http://www.example.com/%1/? [R=301,L]

Note that the last "country" RewriteCond must not have an [OR] flag on it.

The question mark at the end of the substitution URL clears the query string. It will not appear in the redirected URL.

The ">" character in the RewriteConds is arbitrary; It has no special meaning to mod_rewrite or to the regular expressions matching engine, but serves only as a delimiter between the country-match string and the back-referenced country code. I picked it because it is relatively unique and because it implies concatenation.

If you have a huge list of countries, look into using a RewriteMap -- See the Apache mod_rewrite documentation.

Jim

[edit] Corrections as noted below. [/edit]

[edited by: jdMorgan at 10:33 pm (utc) on June 18, 2009]

prof_ethan

10:12 pm on Jun 18, 2009 (gmt 0)

10+ Year Member



Thanks for the quick replay Jim!

However, im still facing issues:
For example, in first rule:
RewriteCond %{QUERY_STRING} ^request_type=default&country=us$
RewriteRule ^/yy/zz/request$ http://www.example.com/us/

Im getting a redirect to:
http://www.example.com/?request_type=default&country=us

As you can see, im getting the whole request and the two variables after the URL.
I tryied changing and applying REG expressions, but i think that the "&" characters is bringing me problems.

Any further tip?

And I was thinking.....
Do apache see this "dinamic pages"?
or it must be an absolute, fixed URL page?

Thanks again!

jdMorgan

10:34 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I messed up the code above, which has now been corrected.

Jim

prof_ethan

12:10 am on Jun 19, 2009 (gmt 0)

10+ Year Member



This is absolutely GREAAAAAAAAT!

btw, i never realize what does [R=301,L] means?

jdMorgan

12:27 am on Jun 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> what does [R=301,L] means?

Answers to all mysteries here [httpd.apache.org].

Jim

prof_ethan

3:16 am on Jun 19, 2009 (gmt 0)

10+ Year Member



so basically R is for redirect, 301 the http answer permanent, L is the last rule, to stop matching here or for stop rewritting the whole url?

man, you absolute rock!
lot of greetings from Argentina!

Caterham

12:54 pm on Jun 19, 2009 (gmt 0)

10+ Year Member



If you plan to transform 100 countries this way, use a RewriteMap.