A better way to use mod_rewrite & php in translating pages?

时间:2015-09-14 15:57:18

标签: php apache .htaccess mod-rewrite

I am trying to translate these pages from English to French:

  • /about.php
  • /contact.php
  • /paintings.php?id={$id}&title={$title}

I created the following link:

<a href="<?php echo $url; ?>"> <?php if($language !== 1): ?>English<?php else: ?>French<?php endif; ?></a>

With this code behind it:

$url = $_SERVER["REQUEST_URI"];                                                     
if (isset($_GET['lang'])) {
    $pattern1 = '&lang=' . $_GET['lang'];
    $pattern2 = '?lang=' . $_GET['lang'];
    $patternTotal = array($pattern1, $pattern2);
    $url = str_replace($patternTotal, '', $url);
    $language = 1;
} 
else if (strpos($_SERVER["REQUEST_URI"], '?')) {
    $url .= '&lang=en';
    $language = 0;
} 
else {
    $url .= '?lang=en';
    $language = 0;
}

What I want for it to do is to return 1 to $language if the lang parameter is set to any value. Then I will use an if statement to fetch and display the content accordingly.

However I realized that this url /paintings.php?id={$id}&title={$title} looks bad and I used mod_rewrite to get it to this version /{$id}/{$title} with this RewriteRule ^([^/]*)/([^/]*)$ /photo.php?id=$1&title=$2 [L]. And things broke. While the $url works on all the other pages on the paintings one it doesn't.

I have a feeling that I got it all wrong. How can I make it work? Also, is there a more efficient way in doing this?

0 个答案:

没有答案