Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite with multiple domain and languages

         

vdelbarco

11:17 am on Sep 17, 2014 (gmt 0)

10+ Year Member



Hello, nice to meet you all.

I crawled a lot of topics forums and i'm not able to find a solution to a mod_rewrite request i'm currently trying to fullfill.

I was sent by this forum by a friend.

I have a multi domain site:

www.example.es
www.example.com
www.example.co.uk .. and so

And i have a multilanguage app with the following URI:

/myshop/h-de/de

Currently every domain responds to all languagues so you can ask for the following and will be processed normally:

www.example.es/myshop/h-de/de --> will serve contain on german from spanish site.

So i need to redirect all the content (with some exceptions, but i'll not explain about that right now) from the language requested to the correct site.

www.example.es/h-de/de -> www.example.de/h-de/de

So i wrote something like this:

RewriteCond %{SERVER_NAME} .*(example).*$ [NC] ----> Captures URL

RewriteCond %{REQUEST_URI} .*\/h-(.{2}/).* [NC] ----> Capture the language after h-
RewriteRule ^.*\/h-(.{2}/).* http://www.example.$1 [R=301,NE,QSA,L] ----> So, i redirect to the right domain.
RewriteRule ^/$ /webapp/wcs/stores/servlet [P,NE,QSA,L] -> i redirect any / to the servlet (forget this if you want)



But it loooooooops. As long as i'm not able to capture the .xx on the domain to check if .xx on the domain and the h-xx matches and skip the rule.

Is this possible to fullfill?

"Help me WebMasterWorld, you're our last chance"

lucy24

7:54 pm on Sep 17, 2014 (gmt 0)

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



It looks as if you need another RewriteCond that says

RewriteCond %{HTTP_HOST} !this-exact-form

so the rule only kicks in when the domain name is something other than the desired form. So one rule for .de, another for .es and so on. It may or may not be practical to collapse them all into a single rule, expressing the condition as
example\.$1
using the /de/ or /es/ or whatever element from the URL.

Are the rules lying loose in the config file (as implied by the form ^/$) or is this happening in htaccess?

vdelbarco

8:52 am on Sep 18, 2014 (gmt 0)

10+ Year Member



Mmm.. thx, but i think i didn't understand you.
So you mean to set an additional RewriteCond just bellow the REQUEST_URI?

vdelbarco

9:08 am on Sep 18, 2014 (gmt 0)

10+ Year Member



I've been thinkg again ... is possible to capture on a ENV variable just the last part of the HTTP_HOST?
That is www.example.xx, just the xx?

lucy24

4:31 pm on Sep 18, 2014 (gmt 0)

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



So you mean to set an additional RewriteCond just below the REQUEST_URI?

Before or after, so long as it's within the same ruleset.

is possible to capture on a ENV variable just the last part of the HTTP_HOST?
That is www.example.xx, just the xx

You can definitely do it in mod_rewrite. (Yes, mod_rewrite can set environmental variables-- and cookies-- and lots of other stuff one doesn't associate with mod_rewrite.) Were you thinking of doing it in mod_setenvif?

:: detour to pore over docs ::

An HTTP request header field (see RFC2616 for more information about these); for example: Host, User-Agent, Referer, and Accept-Language. A regular expression may be used to specify a set of request headers.
...
Since version 2.0.51 Apache will recognize occurrences of $1..$9 within value and replace them by parenthesized subexpressions of regex.

So yes, you could also do it in mod_setenvif. Either separately

SetEnvIf Host example\.de hostname=de
SetEnvIf Host example\.es hostname=es

or via a Regular Expression:

SetEnvIf Host example\.(de|es) hostname=$1

But why would you want or need to use an environmental variable, if you can look right at the host and take immediate action?


Well. That was useful. I never knew mod_setenvif could use captures.

vdelbarco

10:42 am on Sep 22, 2014 (gmt 0)

10+ Year Member



I think the SetEnv was not a good idea, thanks.

Right now i implementes something that looks promising:

RewriteCond %{SERVER_NAME} .*(de)$ [NC]
RewriteCond %{REQUEST_URI} !(/h-{de}/) [NC]
RewriteRule ^.*/h-(.{2}/).* http://int.example.$1

This wokrs in some way. It redirects to the correct language Ejem: int.example.de/shop/h-es -> redirects to int.example.es/h-es. But enters loop if i enter to int.example.de/shop/h-de
Looks like the:
RewriteCond %{REQUEST_URI} !(/h-{de}/) [NC] is not yet correct.

lucy24

4:02 pm on Sep 22, 2014 (gmt 0)

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



What are the braces ("curly brackets") for? And what about the literal parentheses? I don't see any in the example URL, so it seems as if these would simply make the rule superfluous (a ! negative followed by something that never occurs).

The element
.*
without opening anchor is unnecessary. What you do need in this location is a literal period right before the .de. You can also leave off the parentheses, since you couldn't capture here even if you wanted to. Finally, why is it Server Name instead of Host?

phranque

9:05 pm on Sep 22, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, vdelbarco!


SERVER_NAME is the hostname of the virtual server but figuring out which hostname is provided in some cases can be complicated.

http://httpd.apache.org/docs/current/vhosts/mass.html#combinations
A couple of things need to be determined from the request in order to make the dynamic virtual host look like a normal one. The most important is the server name, which is used by the server to generate self-referential URLs etc. It is configured with the ServerName directive, and it is available to CGIs via the SERVER_NAME environment variable. The actual value used at run time is controlled by the UseCanonicalName setting. With UseCanonicalName Off, the server name is taken from the contents of the Host: header in the request. With UseCanonicalName DNS, it is taken from a reverse DNS lookup of the virtual host's IP address. The former setting is used for name-based dynamic virtual hosting, and the latter is used for IP-based hosting. If httpd cannot work out the server name because there is no Host: header, or the DNS lookup fails, then the value configured with ServerName is used instead.