简体   繁体   English

htaccess mod_rewrite不起作用+可以吗?

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

I've been trying for the past hour to rewrite the following url using mod_rewrite in htaccess. 在过去的一个小时中,我一直在尝试使用htaccess中的mod_rewrite重写以下网址。 Original url http://localhost/index.php?Part=main&page=download Desired url http://localhost/index.php?Part=/Main/Download 原始网址http://localhost/index.php?Part = main&page = download所需的URL http://localhost/index.php?Part = / Main / Download

I do remember from the past that htaccess didn't work on on my pc for some reason.. Platform: Windows 7 Ultimate 64bit. 我确实记得过去,由于某些原因,htaccess在我的电脑上无法正常工作。平台:Windows 7 Ultimate 64bit。 Software: Apache - Xampp. 软体:Apache-Xampp。

I also checked httpd.conf and I couldn't find anything wrong with it,it is set to "AllowOverride All"... Any idea what's the problem might be? 我还检查了httpd.conf,但找不到任何错误,它设置为“ AllowOverride All” ...知道是什么问题吗? And is it even possible to rewrite it the way I mentioned above (file.php?Part=/$1/$2 instead of file.php?Part=$1&page=$2)? 甚至可以用我上面提到的方式重写它(file.php?Part = / $ 1 / $ 2而不是file.php?Part = $ 1&page = $ 2)吗?

Thank you for your time, I really do appreciate it! 谢谢您的宝贵时间,我真的很感激!

Pasted the following code in my htaccess and it has returned an error 500 将以下代码粘贴到我的htaccess中,并返回了错误500

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

If it can be done in PHP, please explain how. 如果可以用PHP完成,请说明如何做。 Contents of index.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();
?>

You can re-write in whatever url style you wish, it also has regex in which you can pretty much do anything. 您可以使用任何所需的url样式进行重写,它还具有regex,您几乎可以执行任何操作。

Drop this rule in the .htaccess file 将此规则放在.htaccess文件中

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

In addition, set: 另外,设置:

  <Directory ...> 
  AllowOverride none

to

<Directory ...> 
AllowOverride all

If it's the opposite the settings in .htaccess file will have no effect, it'll look for settings in the httpd.conf file. 如果相反,则.htaccess文件中的设置无效,它将在httpd.conf文件中查找设置。

If you don't want .htaccess involved or having difficulties with it you can insert the rule directly into your virtualhost for an example: 如果您不想涉及.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>

Your rule: 您的规则:

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

has too many spaces. 空间太多。 Apache is kind of obtuse about how it parses directives, so when it sees a space it assumes a new parameter. Apache对解析指令的方式有点呆板,因此,当看到空格时,它将假定一个新参数。 So in your case, apache assumes that http://localhost/index.php?Part=/Main/Download$1 are the rewrite flags since it's the 3rd parameter of a RewriteRule , and obviously, those aren't valid flags. 因此,在您的情况下,apache假定http://localhost/index.php?Part=/Main/Download$1是重写标志,因为它是RewriteRule的第3个参数,并且显然,这些不是有效标志。

Additionally, you can't match against the query string in a rewrite rule, you need to match against the %{QUERY_STRING} variable. 此外,您无法在重写规则中与查询字符串匹配,而需要与%{QUERY_STRING}变量匹配。 You can then use the % backreferences to reference groupings in that match. 然后,您可以使用%反向引用来引用该匹配项中的分组。 Try something like: 尝试类似:

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