duanhuren5581 2013-12-07 20:32
浏览 48
已采纳

为所有网站URL添加语言的聪明方法?

In this year i wrote a very big website, with no CMS. I wrote it from scratch. Today i'm starting to implement a translation system.

The idea is, basically, to add /language/ inside the URL. This means if the index page, when i'm browsing on default language, has the url

http://www.racebooking.net/index.php

once the user clicks on the english flag in order to view the english version of the website, the URL will become

http://www.racebooking.net/en/index.php

in .htaccess, then, i have a rule which converts /en/ into ?lang=en.

The problem

Every page has tons of links, which means i have thousands of <a href=".."></a> tags in my website.

For example, on my index.php page, i have a link like this one

<a href="http://www.racebooking.net/forum">Forum</a>

which, if i am currently browsing the english version, should point to

http://www.racebooking.net/en/forum

At first sight, the idea was to change it to

<a href="http://www.racebooking.net/<?php echo $_GET['lang'] . '/';?>forum">Forum</a>

which works pretty good! But, the problem is i have thousands of <a> tags on my website!!! it would be totally insane to change them all, for just adding <?php echo $_GET['lang'] . '/';?> in them!! It would take days and days and days of work.

So, is there a smart way to add /language/ to all my website URLS?

EDIT

I would prefer not using any js. All the users with js disabled will encounter problems while browsing my website!

  • 写回答

5条回答 默认 最新

  • duanjuelu8874 2013-12-07 20:43
    关注

    The HTTP permanent redirect status code (301) is exactly for your case. All links to your site will still work, and additionally any bookmarks will be updated to your new url path construct.

    You can set it up in Apache using mod_rewrite:

    RewriteRule /(?!en|es|de|fr|it)(/?.*)$ http://www.mydomain.com/en/$1 [R=301,L]
    

    This will redirect every url path, which does not start with either en, es, de, fr, it to the en (default) sub path.

    Edit to comments:

    The solution above would enable for all your links to work (old and new ones).

    If you additionally want to change all the links on your page, so that search engines will see them, then your only option is to rewrite them in your PHP/HTML code (using JavaScript for this is a bad idea for many reasons).

    The best option in this case is to write a helper function, which generates the links for you depending on current language. This way you will have a single point, where you can change your links, should they change once again in the future.

    Unfortunately, you won't be able to change all of your links at once. If you have thousands of them, and no common code which generates all of them, then you have to do this one by one. Good news is, that you are not the first one with this problem, and the developers of professional IDE's already implement tools to aid you. My personal choice is a commercial software, but other open sourced IDE's also have a pretty good find/replace options. You can for instance write a regex, which will find your links and replace them accordingly to the rules you provide. To write a good regex replacer might prove to be very beneficial as opposed to reviewing all links one by one.

    This is one of the possible implementation of the url helper:

    class UrlHelper
    {
        public static function make($base, $lang = null)
        {
            if ( $lang === null ) {
                $lang = 'en';
                if ( isset($_GET['lang']) ) {
                    $lang = $_GET['lang'];
                }
            }
            $url = "http://www.racebooking.net/{$lang}/{$base}";
            return $url;
        }
    }
    

    Now you have to find all places where links are outputed and use the helper:

    <a href="<?php echo UrlHelper::make('/old/url'); ?>">Link</a>
    

    Note that this is just an example of how it could work. I actually don't recommend to implement the helper as a static method, because static is pure evil. For your real implementation you might consider a helper object instead of helper method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)