繁体   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