Htaccess别名重定向伪造的多语言网址

时间:2012-08-26 12:17:27

标签: .htaccess mod-rewrite url-rewriting

我有一个带有这种网址的域名www.domain.com

www.domain.com/my-italian-page.html (已由其他htaccess规则重写)

我想创建一个假的多语言网址,如

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

用户将在地址栏中看到重写者网址 www.domain.com/my-english-page.html ,但我要显示的内容是原始 www.domain.com/my-italian-page.html

我在共享服务器上,所以我不能使用apache vhost规则,所以我必须通过htaccess找到解决方案。

有人可以帮我找到正确的方法吗?

由于

2 个答案:

答案 0 :(得分:1)

所以你想要英文网址指向意大利语内容?希望你生成这些重写规则的php脚本能够进行翻译。但是你要为每个页面执行此操作:

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

每个页面。

答案 1 :(得分:-1)

我相信有一个不太复杂的解决方案。这实际上是大多数CMS在启用SEO URL时的工作方式

  • 重写任何网址(mydomain/anytext.html [实际上您不应该使用.html扩展名])到脚本(例如mydomain.tld/translate.php

  • 使用$_SERVER['PATH_INFO']的内容(应包含anytext.html)来显示正确的页面

  • 如果页面不存在,请设置正确的HTTP响应代码:http_response_code(...)(请参阅本答案的结尾,对于5.4以下的php5上的函数:PHP: How to send HTTP response code?

示例.htaccess(实际上最初“被盗”并且从错字3设置中严重修改)

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]

非常基本的伪代码(未经测试,可能存在语法错误)示例:

<?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');
?>

我在你的回答中看到你有另一个脚本 - dispatcher.php - 你似乎不愿意修改它。我相应地修改了我的响应,但请记住,到目前为止最简单的方法是修改现有脚本以处理任何英语路径。

相关问题