htaccess文件的URL重写未创建所需的URL

嗨,我正在创建一个多语言的php网站。我想创建一个像www.domain.com/en和www.domain.com/sp这样的URL,因为我的URL结构是www.domain.com/index.php?lang=en

请指教。

这是我的网站代码:

htaccess:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteRule ^lang/([^/]*)$ /multi_lang_php/index.php?lang=$1 [L]


</ifModule>

config.php:

<?php


session_start();
if (!isset($_SESSION['lang']))
    $_SESSION['lang'] = "en";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
    if ($_GET['lang'] == "en")
        $_SESSION['lang'] = "en";
    else if ($_GET['lang'] == "sp")
        $_SESSION['lang'] = "sp";
}

require_once "language/" . $_SESSION['lang'] . ".php";

英语/西班牙语语言文件是这样的:

    <?php

$lang = array(
    "title" => "PHP Multilang Demo",
    "banner" => "MLT",
    "home" => "Home",
    "pricing" => "Pricing",
    "contact" => "Contact",
    "description" => "This is explanation about my website.",
    "lang_en" => "English",
    "lang_sp" => "Spanish",
);

我的index.php:

<?php
include "config.php";
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $lang['title'] ?></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <style>
        .footer {
            left: 0;
            position: fixed;
            bottom: 0;
            text-align: center;
            color: white;
            width: 100%;
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#"><?php echo $lang['banner'] ?></a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarText">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Features</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Pricing</a>
                </li>
            </ul>
            <span class="navbar-text">
                Navbar text with an inline element
            </span>
        </div>
    </nav>
    <div class="container" style="margin-top: 100px">
        <div class="row justify-content-center">
            <div class="col-md-6 col-md-offset-3 text-center">
                <h1>PHP Multilang Website</h1>
                <p>This is a demo website for Multilanguage website</p>
            </div>
        </div>
    </div>
    <div class="footer bg-dark">
        <a href="en"><?php echo $lang['lang_en'] ?></a>|
        <a href="sp"><?php echo  $lang['lang_sp'] ?></a>
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>

</html>
评论
  • nquis
    nquis 回复

    You need part with RewriteBase /.

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(.+?)/.*$  /multi_lang_php/index.php?lang=$1 [L]
    

    它将URL转换为:

    http://www.example.com/en/
    

    变成:

    http://www.example.com/multi_lang_php/index.php?lang=en