htaccess mod_rewrite不起作用+是否可能?

时间:2015-03-31 17:54:41

标签: php apache .htaccess mod-rewrite xampp

我在过去一小时内一直在尝试使用htaccess中的mod_rewrite重写以下网址。 原始网址http://localhost/index.php?Part=main&page=download 所需网址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();
?>

2 个答案:

答案 0 :(得分:0)

你可以重写你想要的任何网址样式,它也有正则表达式,你几乎可以做任何事情。

将此规则放在.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>

答案 1 :(得分:0)

你的规则:

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]
相关问题