如何翻译URL slug并将其应用于htaccess

时间:2015-04-08 10:19:53

标签: php .htaccess url rewrite translation


可以通过 htaccess php 翻译URL slug吗? 例如:

http://www.example.com/sr/Arheoloski-lokaliteti.html --> index.php?lang=sr

点击带有EN标志的图标,然后重定向到此处:

http://www.example.com/en/Arheoloski-locality.html --> index.php?lang=en

Arheoloski-lokaliteti 自动转换为 Arheoloski-locality (通过API或其他东西)但是如何在其上应用htaccess重写?

1 个答案:

答案 0 :(得分:0)

试试这个

    RewriteEngine On
     # The following rule tells Apache that if the requested filename
     # exists, simply serve it.
     RewriteCond %{REQUEST_FILENAME} -s [OR]
     RewriteCond %{REQUEST_FILENAME} -l [OR]
     RewriteCond %{REQUEST_FILENAME} -d
     RewriteRule ^.*$ - [NC,L]
     RewriteRule ([^\/.]*).*\/(.*).html$ index.php?lang=$1&page=$2 [NC,L]

然后是index.php

    <?php
        $language = isset($_GET['lang']) ? $_GET['lang'] : "default";
        $page = isset($_GET['page']) ? $_GET['page'] : "default";
        switch($language){
              case "en":
                  //logic to pull appropriate content based on $page value 
                  break;

              case "sr":
                  //logic to pull appropriate content based on $page value
                  break;

              default:
                  //logic to pull appropriate content based on $page value
                  break;
        }
     ?>