Home htaccess RewriteRule passes index.php instead of string
Reply: 1

htaccess RewriteRule passes index.php instead of string

MathewT
1#
MathewT Published in 2017-12-06 15:58:26Z

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.

anubhava
2#
anubhava Reply to 2017-12-06 17:27:26Z

You must skip existing files and directories from your rewrite rule to avoid looping the rule:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+?)/?$ index.php?page=$1 [L,QSA]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.34186 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO