1

I have a domain www.domain.com with this kind of url

www.domain.com/my-italian-page.html (already rewritten by others htaccess rules)

I'd like to create a fake multilanguage url like

www.domain.com/my-english-page.html

The user will see in the address bar the rewritter url www.domain.com/my-english-page.html but the content that I'd like to show is the original www.domain.com/my-italian-page.html .

I'm on a shared server so I can't use apache vhost rule so I have to find a solution via htaccess.

Someone could help me to find the right way?

Thanks

luigi
  • 11
  • 1
  • Do you want a static rewrite like always translate "la mia vacanza recente" (don't hit me I used google translate) into "my recent vacation" or do you want dynamic rewriting for multiple articles? – dualed Aug 26 '12 at 12:39
  • To be more precise: is an ecommerce. So I have to recreate a rewrite foreach product url. – luigi Aug 26 '12 at 12:57
  • Example I have in ITALIAN www.mydomain.com/burro.html My idea is to create a php script that will write into htaccess one single rule foreach product to get something like in fake ENGLISH www.mydomain.com/butter.html So if the user will visit www.mydomain.com/butter.html i'd like that url showed on address bar will be /butter.html but the content is from the original burro.html Obviously i will save both clean url in a database then i will call the php script to write the rule into .htaccess. – luigi Aug 26 '12 at 13:06

2 Answers2

1

So you want english URLs pointing to italian content? Hope your php script that generates these rewrite rules does the translating. But you'd do this for each one of your pages:

RewriteRule ^/?english-page.html$ /italian-page.html [L]

for each one of your pages.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Yes, the php script translate everything except url. I think that some other rule is causing problem. – luigi Aug 26 '12 at 18:25
  • My htaccess `Options +FollowSymLinks -MultiViews -Indexes RewriteEngine On RewriteBase /acquista/ RewriteCond %{REQUEST_URI} !^/acquista/(payment|admin|provider|partner)/ RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^/?butter-of-cacao-biologic.html$ /Burro-di-cacao-biologico.html RewriteRule ^(.*)$ dispatcher.php [L] ` – luigi Aug 26 '12 at 18:29
  • @user1625783 you probably want to put that rule *right after* `RewriteBase /acquista/`, or better yet, in the htaccess file in your document root, since it's `www.domain.com/my-english-page.html` and not `www.domain.com/acquista/my-english-page.html` – Jon Lin Aug 26 '12 at 18:47
0

Generally you want to keep the code executed in the web server as small as possible, so having a rewrite rule for each page is not usually a good idea. I suggest to implement this the way most CMS work when SEO URLs are enabled:

  • Rewrite any url (mydomain/anytext.html [actually you should not use the .html extension either]) to a script (eg. mydomain.tld/translate.php)

  • Use content of $_SERVER['PATH_INFO'] (should contain anytext.html) to display the correct page

  • Set correct HTTP response code if page does not exist: http_response_code(...) (see end of this answer for a function on php5 below 5.4: PHP: How to send HTTP response code?)

Sample .htaccess (actually originally "stolen" and severely modified from a typo3 setup)

RewriteEngine On

# Uncomment and modify line below if your script is not in web-root
#RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) translate.php$1 [L]

Very basic pseudo-code-like (not tested, there may be syntax errors) example:

<?php
// using a database? you have to escape the string
$db = setup_db();
$page = db->escape_string(basename($_SERVER['PATH_INFO']));
$page = my_translate_query($page);

// no database? do something like this.
$trans = array( 'english' => 'italian', 'italian' => 'italian' );
$page = 'default-name-or-empty-string';
if(isset($_SERVER['PATH_INFO'])) {
   if(isset($trans[basename($_SERVER['PATH_INFO'])])) {
      $page = $trans[$trans[basename($_SERVER['PATH_INFO'])]];
   }
   else {
      http_response_code(404);
      exit();
   }
}

// need to redirect to another script? use this (causes reload in browser)
header("Location: otherscript.php/$page");
// you could also include it (no reload), try something like this
$_SERVER['PATH_INFO'] = '/'.$page;
// you *may* have to modify other variables like $_SERVER['PHP_SELF']
// to point to the other script
include('otherscript.php');
?>

I saw in your answer that you have another script - dispatcher.php - which you seem reluctant to modify. I modified my response accordingly, but please keep in mind that by far the easiest way would be to just modify the existing script to handle any English paths itself.

dualed
  • 10,262
  • 1
  • 26
  • 29