簡體   English   中英

htaccess mod_rewrite不起作用+可以嗎?

[英]htaccess mod_rewrite does not work + is it possible?

在過去的一個小時中,我一直在嘗試使用htaccess中的mod_rewrite重寫以下網址。 原始網址http://localhost/index.php?Part = main&page = download所需的URL http://localhost/index.php?Part = / Main / Download

我確實記得過去,由於某些原因,htaccess在我的電腦上無法正常工作。平台:Windows 7 Ultimate 64bit。 軟體:Apache-Xampp。

我還檢查了httpd.conf,但找不到任何錯誤,它設置為“ AllowOverride All” ...知道是什么問題嗎? 甚至可以用我上面提到的方式重寫它(file.php?Part = / $ 1 / $ 2而不是file.php?Part = $ 1&page = $ 2)嗎?

謝謝您的寶貴時間,我真的很感激!

將以下代碼粘貼到我的htaccess中,並返回了錯誤500

Options +FollowSymlinks
RewriteEngine on
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]

如果可以用PHP完成,請說明如何做。 index.php的內容:

<?php 
session_start();
# Disable Notices

if(!file_exists('assets/config/install/installdone.txt')){
    header("Location: assets/config/install/install.php");
    exit;
} else {
    # Get Database Information
    require_once("assets/config/database.php");

    require_once("assets/config/properties.php");
    require_once("assets/config/afuncs.php");
    # Define $getbase variable
    $getbase = isset($_GET['Part']) ? $_GET['Part'] : "";

    switch($getbase){
        case NULL:
            header('Location: ?Part=main');
            break;
        case "main":
            $getslug = $mysqli->query("SELECT slug from ".$prefix."pages");
            while($fetchslug = $getslug->fetch_assoc()) {
                $slugarray[] = $fetchslug['slug'];
            }
            include("sources/structure/header.php");
            include("sources/structure/theme/left.php");
            include("sources/structure/theme/right.php");
            include("sources/public/main.php");
            include("sources/structure/footer.php");
            break;
        case "ucp":
            include("sources/structure/header.php");
            include("sources/ucp/main.php");
            include("sources/structure/footer.php");
            break;
        case "admin":
            include("sources/structure/admin/header.php");
            include("sources/admin/main.php");
            break;
        case "gmcp":
            include("sources/structure/header.php");
            include("sources/gmcp/main.php");
            include("sources/structure/footer.php");
            break;
        case "misc":
            include("sources/misc/main.php");
            break;
        default:
            include("sources/structure/header.php");
            include("sources/public/main.php");
            include("sources/structure/footer.php");
            break;
    }
}

$mysqli->close();
?>

您可以使用任何所需的url樣式進行重寫,它還具有regex,您幾乎可以執行任何操作。

將此規則放在.htaccess文件中

Options +FollowSymlinks
RewriteEngine on
rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]

另外,設置:

  <Directory ...> 
  AllowOverride none

<Directory ...> 
AllowOverride all

如果相反,則.htaccess文件中的設置無效,它將在httpd.conf文件中查找設置。

如果您不想涉及.htaccess或遇到困難,可以將規則直接插入虛擬主機中,例如:

<VirtualHost *>
  ServerName www.example.com
  RewriteEngine on
  rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]
</VirtualHost>

您的規則:

rewriterule ^index.php?Part=main&page=download (.*)$ http://localhost/index.php?Part=/Main/Download$1 [r=301,nc]

空間太多。 Apache對解析指令的方式有點呆板,因此,當看到空格時,它將假定一個新參數。 因此,在您的情況下,apache假定http://localhost/index.php?Part=/Main/Download$1是重寫標志,因為它是RewriteRule的第3個參數,並且顯然,這些不是有效標志。

此外,您無法在重寫規則中與查詢字符串匹配,而需要與%{QUERY_STRING}變量匹配。 然后,您可以使用%反向引用來引用該匹配項中的分組。 嘗試類似:

RewriteCond %{QUERY_STRING} ^Part=([^&]+)&page=([^&]+)
RewriteRule ^index\.php$ /index.php?Part=/%1/%2 [L,R]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM