douyinliu8813 2010-11-30 04:15 采纳率: 100%
浏览 47
已采纳

使用.htaccess或php进行URL美化?

In search of a more userfriendly & search engine friendly urls, i want have beautied my urls: The htacces apache rule that achieves this (Thanks to Laurence Gonsalves)

RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]

which makes this possible:
/uk/somepage instead of /somepage?ln=uk
/de/somepage instead of /somepage?ln=de
/ja/somepage instead of /somepage?ln=ja

Now the difficult part: previously, the url was replaced with a normal link like href="?ln=de" or href="?ln=it" for changing language of the current page. But now how can i achieve that? Sothat the current page stays the same, but only the preceding two lowercase letters that say to the browser what language it is in change?

So how to tell the link to only change the /uk/contact to /de/contact once the german (de) language flag is clicked? php solution to rewrite the url or htaccess solutions are accepted.

I found out that $_SERVER['REQUEST_URI'] will output /uk/somepage but i cant write the php code that can split up the components, add a new language code like "de" into it, which i can put manually into a normal href that goes on a German flag. etc. Thanks for any and all clues/answers!

  • 写回答

2条回答 默认 最新

  • dongpengyu1363 2010-11-30 05:40
    关注

    You'd probably want to look at something like explode or regular expressions to strip out the non-language part of the URL (e.g., /contact) and just add it again to a new string containing the language identifier.

    Maybe this could get you started:

    <?php
    
    function changeLanguageLink($language_id)
    {
        $uri = $_SERVER['REQUEST_URI'];
    
        $link = preg_replace('/\/?(uk|de)\/(.*)/', "/$2", $uri);    
        $link = $language_id . $link;
    
        return $link;
    }
    
    ?>
    
    <a href="changeLanguage('uk');">Change language to UK</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路