Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite loop

         

poisons

5:42 pm on Feb 9, 2010 (gmt 0)

10+ Year Member



Hi All,
I've a little problem getting me crazy.

I've a unique server and unique file, for istance how_it_works.php.
I've more domains pointing the same server, so for example:
call from www.myserver.co.uk --> go to how_it_works.php
call from www.myserver.es --> go to how_it_works.php

In how_it_works.php I check the domain extension (country based) and change the appropriate language. Now, I would like to have different filenames language-based that point to the same file, of course.

For example:
If the domain ends with .es, I want to call the file como_funciona.php but the server has to call internally the file how_it_works.php

I want to use the .htaccess and the rewrite mod.
Till here is pretty simple:

RewriteCond %{HTTP_HOST} ^mysite\.es
RewriteRule ^como_funciona.php how_it_works.php [L]


But, at the sametime I want to tell search engines that the page how_it_works.php in the .es domain has moved to como_funciona.es, so:

RewriteCond %{HTTP_HOST} ^mysite\.es
RewriteRule ^como_funciona.php how_it_works.php
RewriteRule ^how_it_works.php como_funciona.php [R=301, L]


But is this correct?
Doesn't go looping?
I've to do the same with the n other domains...

Thanks in advance

jdMorgan

11:42 pm on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Externally redirect [i]direct client requests only[/i] for
# URL example.es/how_it_works.php to URL example.es/como_funciona.php
RewriteCond %{HTTP_HOST} ^example\.es
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /how_it_works\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^how_it_works\.php$ http://www.example.es/como_funciona.php [R=301,L]
#
# Internally rewrite requests for URL /example.es/como_funciona.php
# to script filepath /how_it_works.php
RewriteCond %{HTTP_HOST} ^mysite\.es
RewriteRule ^como_funciona\.php how_it_works.php [L]

By checking the client's HTTP request (exactly a seen in your raw server access log file), you avoid redirecting how_it_works.php *file requests* resulting from the internal rewrite of the como_funciona.php URL to the how_it_works.php filepath. Hopefully, the specific rule comments will clarify this a bit.

Jim

poisons

9:54 am on Feb 10, 2010 (gmt 0)

10+ Year Member



Thanks!

So, for two domains, (.es, .fr):

# Externally redirect direct client requests only for 
# URL example.es/how_it_works.php to URL example.es/como_funciona.php
RewriteCond %{HTTP_HOST} ^example\.es
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /how_it_works\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^how_it_works\.php$ http://www.example.es/como_funciona.php [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.fr
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /how_it_works\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^how_it_works\.php$ http://www.example.fr/comment_faire.php [R=301,L]
#
# Internally rewrite requests for URL /example.es/como_funciona.php
# to script filepath /how_it_works.php
RewriteCond %{HTTP_HOST} ^example\.es
RewriteRule ^como_funciona\.php how_it_works.php [L]
#
RewriteCond %{HTTP_HOST} ^example\.fr
RewriteRule ^comment_faire\.php how_it_works.php [L]



Right?

[edited by: jdMorgan at 12:12 pm (utc) on Feb 10, 2010]
[edit reason] Please use example.com only. [/edit]

poisons

10:09 am on Feb 10, 2010 (gmt 0)

10+ Year Member



Hmmm, jim, it seems there's something wrong.
when I call the page example.es/como_funciona.php I simply have a 404 error

Here the .htaccess I have:

Order Allow,Deny
#Deny from 151.**.221.940
Allow from all

ErrorDocument 404 /404.php

RewriteEngine on
RewriteRule ^robots.txt robots.php

RewriteCond %{HTTP_HOST} ^example\.es
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /come_funziona\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^come_funziona\.php$ http://www.example.es/como_funciona.php [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.es
RewriteRule ^como_funciona\.php come_funziona.php [L]

[edited by: jdMorgan at 12:31 pm (utc) on Feb 10, 2010]
[edit reason] Please use example.com only [/edit]

jdMorgan

12:27 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two problems seem obvious:

First, come_funziona and como_funciona are not the same, so a rule for one will not apply to the other -- It looks like Spanish and Italian are confused here.

Second, example.com won't match www.example.com, so you need to be consistent with the exact hostname. (I suggest that you pick either the www- or non-www subdomain as the canonical hostname and link to and redirect to only that canonical hostname. Then after you get this code working, add a 301 redirect from the non-canonical hostname to the canonical.)

When posting your code, please use "example.com", "example.it", "example.es", "example.fr" etc. -- We do not generally allow links to "real" domains here -- See our Terms of Service and our Forum Charter. Also, if you use any other domain, the forum's auto-linking function will make a mess of your post. In either case, I will have to edit it.

Does your pre-existing robots.txt rewrite rule work properly on this server?

Jim

poisons

1:05 pm on Feb 10, 2010 (gmt 0)

10+ Year Member



Hi jim,
thanks for your answer.
come_funziona.php is the master, (I used how_it_works.php in the first post but the logic remain the same). Instead of example I used mysite, that is not a real address, anyway I'll use example from now on.

The problem remains.
If the come_funziona.php is the master file, the real one, I want to mask the non-existent files comment_faire.php (french) and como_funciona.php (spanish)

If I write the rules as you posted on the #2 post it results as a 404 error, you suggest for the www in front of it?.

So I shoud write:

RewriteCond %{HTTP_HOST} ^example\.es 
RewriteCond %{HTTP_HOST} ^www\.example\.es
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /come_funziona\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^come_funziona\.php$ http://www.example.es/como_funciona.php [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.fr
RewriteCond %{HTTP_HOST} ^www\.example\.fr
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /come_funziona\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^come_funziona\.php$ http://www.example.fr/comment_faire.php [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.es
RewriteCond %{HTTP_HOST} ^www\.example\.es
RewriteRule ^como_funciona\.php come_funziona.php [L]
#
RewriteCond %{HTTP_HOST} ^example\.fr
RewriteCond %{HTTP_HOST} ^www\.example\.fr
RewriteRule ^comment_faire\.php come_funziona.php [L][/


Is this right?
Thanks in advance,
giovanni

jdMorgan

1:55 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must rewrite to the "real file" if you want this to work.

You should do the domain canonicalization with a separate rule, so that the other rules do not have to handle multiple hostnames, and so that your www/non-www page URLs don't compete against themselves for search ranking.

Also, your 'spelling' of these URLs and filepaths must be absolutely correct and utterly-consistent, or the code will not work.

I suggest that you stop trying to add functionality before you get one of these redirect/rewrite pairs working. Test with simple code, get that working, then add one more function, test, correct, test, add more code if working, repeat. The chances of debugging a big file are much less that debugging a small one...

For now, I will 'support' only the Spanish domain. We can discuss french later after getting the Spanish-page code working. I will also assume that you use and link only to the www subdomains.

# Externally redirect direct client requests for script filepath to language-specific URL
RewriteCond %{HTTP_HOST} ^www\.example\.es
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /come_funziona\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^come_funziona\.php$ http://www.example.es/como_funciona.php [R=301,L]
#
# Externally redirect non-canonical non-www hostname requests to canonical www hostname
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+\.(co\.)?[a-z]{2})\.?(:[0-9]+)?$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#
# Internally rewrite language-specific URL requests to script filepath
RewriteCond %{HTTP_HOST} ^www\.example\.es
RewriteRule ^como_funciona\.php come_funziona.php [L]

Note that the pattern in the domain canonicalization rule's rewritecond will support .co.cc formatted domains, such as .co.uk.

Again, does your pre-existing robots.txt rewrite rule work properly on this server?
If not, then the problem is "outside" the code we are discussing, and we need to change our focus temporarily...

Jim

Leosghost

2:41 pm on Feb 10, 2010 (gmt 0)

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



<aside>@OP ..BTW when you do get onto the "French" ..you may want to change "comment faire" ( which actually means "how to do" ..or "how to" ) to "comment ça marche" or "comment ca marche" ( the latter with a non accented c which means "how it works" or "how it goes"..and is what a French speaker would actually say or type.

Just in case you are also looking for true equivalent page titles..</aside>


Please excuse the "interruption" Jim ..:)

jdMorgan

3:01 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Soyez mon invitee, monsieur. -- Merci pour votre explication!

Jim