Forum Moderators: phranque

Message Too Old, No Replies

RewriteMap or RewiteRule

         

speedyone

5:32 pm on Apr 22, 2010 (gmt 0)

10+ Year Member


Hello,

I have been going through all the reg expressions posts and RewriteMap posts trying to find a solution for this issue I have.

I have a bunch of URL's that I need to rewrite and they have some similarities. I just wanted to simplify all this Here is what i could do and it works but I wanted to shrink this all down to not be such a huge file.

RewriteCond %{REQUEST_URI} ^/b2/enUS/client-homepage$ [NC]
RewriteRule .* http://us.mydomain.net/be2/en-us/data/homepage.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/esMX/client-news$ [NC]
RewriteRule .* http://us.mydomain.net/be2/es-mx/data/news.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/esMX/client-homepage$ [NC]
RewriteRule .* http://us.mydomain.net/be2/es-mx/data/homepage.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/esAR/client-news$ [NC]
RewriteRule .* http://us.mydomain.net/be2/es-ar/data/news.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/esAR/client-homepage$ [NC]
RewriteRule .* http://us.mydomain.net/be2/es-ar/data/homepage.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/ptBR/client-news$ [NC]
RewriteRule .* http://us.mydomain.net/be2/pt-br/data/news.xml [L]

RewriteCond %{REQUEST_URI} ^/b2/ptBR/client-homepage$ [NC]
RewriteRule .* http://us.mydomain.net/be2/pt-br/data/homepage.xml [L]

I was thinking I could use something about like this.. But I am very new to the RewriteMap:

rewritemap lowercase int:tolower
RewriteCond %{REQUEST_URI} ^/b2/([a-z]+)([A-Z]+)/feed/news$ [NC]
RewriteRule .* http://us.mydomain.net/be2/$1-${lowercase:$2}/data/news.xml [L]

rewritemap lowercase int:tolower
RewriteCond %{REQUEST_URI} ^/b2/([a-z]+)([A-Z]+)/feed/news$ [NC]
RewriteRule .* http://us.mydomain.net/be2/$1-${lowercase:$2}/data/homepage.xml [L]

Does this look right? I am at a loss what I am doing wrong here.

jdMorgan

12:04 am on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use regular expressions and back-references to their fullest, and do not use a RewriteCond to test REQUEST_URI if the RewriteRule pattern can be used to do so; RewriteConds are not processed at all unless the RewriteRule pattern matches (see mod_rewrite documentation), so efficiency is much better if you use the most-specific RewriteRule pattern possible.

Because it appears that this code is going into a server config file (as opposed to .htaccess), the first seven rules can likely be reduced to something like this:

RewriteRule ^b2/([a-z]{2})([A-Z]{2})/client-([a-z]+)$ http://${lowercase:$2}.example.com/be2/$1-${lowercase:$2}/data/$3.xml [R=301,L]

However, this will redirect all possible language codes, and all possible "page names," and so may be too "accepting" for your needs.

An alternative to the "accept-anything" approach would be to list the alternatives that are acceptable, such as (enUS|esMX|esAR|ptBR) and (homepage|news). Note that the first pattern will then have to be processed further by a RewriteCond to lowercase your pesky uppercase country-codes, and the back-references will need to be adjusted. Something like:

RewriteMap lowercase int:tolower
#
RewriteCond ${lowercase:$1} ^([a-z]{2})([a-z]{2})$
RewriteRule ^/b2/(enUS|esMX|esAR|ptBR)/client-(homepage|news)$ http://%1.example.com/be2/%1-%2/data/$2.xml [R=301,L]

Note that RewriteMap is only required once for each map, no matter how many times you need to invoke that map.

Note also that one advantage of this approach is that it eliminates the multiple calls to the RewriteMap. However, it's still a bit wasteful, in that we know that the first two characters processed by the RewriteCond will already be lowercase. However, avoiding this would require an additional RewriteCond. Everything's a trade-off...

This still may not be selective enough for you, but I trust that the above will demonstrate some useful techniques.

Jim

speedyone

8:50 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



That is very interesting. Didn't even think of using it that way. I will do some testing and get back to you on what I end up using.

g1smd

12:30 am on Apr 24, 2010 (gmt 0)

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



If you have any way of measuring the time taken to process these different types of rules, there will be people interested in the results...