2012-08-16 92 views
0

我需要创建多语言链接到我的网站,我想使用str_replace,但我不知道这是否是正确的选择。也许正则表达式会做的伎俩......但是即时通讯不是精通正则表达式...用PHP更改网址

我的网址是这样的: 本地主机/项目/公/ LANG /事件/ ID/

的朗能成为:

/en/ 
/fi/ 
/sv/ 

这个网址是相同的,否则只是/ LANG /部分网址的变化。 我试图与此更改URL:

<?php 
$search = array("/sv/", "/fi/"); 
$replace = array("/en/"); 
$url_en = str_replace($search, $replace, $url); 
echo $url_en; 
?> 

然而,不给我正确的网址,并将其与SV或网络连接代替所有的东西。我认为正确的解决办法是

+0

@Bhuvan Rikka这是不正确的,你可以看到下面;-) – feeela 2012-08-16 08:37:56

+1

这个东西使用.htaccess文件 – 2012-08-16 08:55:05

回答

0

使用.htaccess文件翻译的frindly网址

创建在根目录网站.htaccess文件

你的链接看起来就像域/ A/b/C/d

本地主机/项目/公共/ LANG /事件/ ID/

这是被翻译为域/的index.php?行动=项目& saction =公共&语言= LANG & taction =事件& TID = ID

他们正在被这个文件到域/的index.php?动作翻译=一个& ID = B & saction = C & SID = d

下面是我的一个文件,作为例子;好运

Options +SymLinksIfOwnerMatch 

RewriteEngine on 


RewriteRule ^([a-zA-Z-]+)/(producator)-([0-9]+)$ index.php?action=$1&producator=$3 [NC,L] 
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producator)-([0-9]+)$ index.php?action=$1&id=$2&producator=$4 [NC,L] 

RewriteRule ^([a-zA-Z-]+)/(producatorx)-([0-9]+)$ index.php?action=$1&producatorx=$3 [NC,L] 
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(producatorx)-([0-9]+)$ index.php?action=$1&id=$2&producatorx=$4 [NC,L] 

RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L] 

RewriteRule ^([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2 [NC,L] 

RewriteRule ^(tag)/([a-z0-9]+)$ index.php?action=tag&tag=$2 [NC,L] 

RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(update)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=update&code=$4 [NC,L] 
RewriteRule ^([a-zA-Z-]+)-([0-9]+)/(delete)-([a-z0-9]+)$ index.php?action=$1&id=$2&saction=delete&code=$4 [NC,L] 

RewriteRule ^([a-zA-Z-]+)-([0-9]+)/([a-zA-Z-]+)-([0-9]+)$ index.php?action=$1&id=$2&saction=$3&sid=$4 [NC,L] 

RewriteRule ^(.*)-([0-9]+).html$ index.php?action=details&id=$2 [NC,L] 
+1

由于你的代表低,不做-1,但你只是转储你正在使用的.htaccess文件之一?我看不出问题中提出的问题,以及你回答的问题。问题是关于更改网址以包含EN,FI,SV等其中之一,但是您正在讨论生产者,操作。要么修改你的答案来解决这个问题,否则人们会对它失望。 – DavChana 2012-08-16 09:58:34

+0

他必须替换一些单词,他必须抓住这个想法;他的链接是怎样的,他们是如何翻译的;我认为这是解决他所有连接问题的正确方法 – 2012-08-16 11:40:40

+0

但即使如此,在某人身上倾倒了13多行Pure htaccess,并没有对每一行进行任何解释,就像是说'嘿,你看!你想要走开!祝你好运。“不管怎样,这只是我的意见,你肯定可以以任何你想要的方式自由回答。 – DavChana 2012-08-16 14:19:18

-3
preg_replace('|(localhost/project/public/)..|', '\1EN', $url); 
+0

使用htaccess的文件,并建立所有的URL结构完成;这是非常有用的,因为如果一个url结构匹配,你知道所有的变量和值 – 2012-08-16 09:01:58