htaccess RewriteRule passes index.php instead of string

时间:2017-12-06 15:58:26

标签: php apache .htaccess mod-rewrite url-rewriting

So I'm trying to learn about .htaccess RewriteRules and am facing the following issue:

"/home" should open "index.php?page=home"

and I have a php $_GET['page'] inside "index.php", but it passes "index.php" instead of "home" every time.

Here are the files

.htaccess

RewriteEngine On
RewriteRule ^/?(.+)/?$ index.php?page=$1 [L]

index.php PHP part

<?php

        if(isset($_GET['page'])) {
            $path = $_GET['page'] . ".php";
            if (file_exists($path)) {
                include($path);
            }
            else {
                echo "File not found: " . $path;
            }
        }
        else {
            include("home.php");
        }

?>

The output is always:

File not found: index.php.php

I face this issue on both: web-server and apache.

Any suggestions? I'll update my question with more info if necessary.

1 个答案:

答案 0 :(得分:0)

您必须跳过重写规则中的现有文件和目录,以避免循环规则:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+?)/?$ index.php?page=$1 [L,QSA]