Seraphendipity
Seraphendipity

Reputation: 170

Include Statements with PHP do not Access .htaccess

When using the Pretty URLS by adding .htaccess tot he root folder, my <a href=""> links of course do not need the .php file extension. However, when I use the PHP require or include, it throws an error and does not seem to use the .htaccess to add the necessary .php, requiring me to add .php to these statements.

.htaccess

# Redirect everything that doesn't match a directory or file to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

The .php file in question:

<!DOCTYPE html>
<head></head>
<body>
    <?php require "../Resources/nav.php";?>
        // Main Content
    <?php require "../Resources/footer.php";?>
</body>

Does anyone know why these statements do not use the .htaccess? Is it just necessary to include file extensions, or is there a way to force them to use .htaccess?

Upvotes: 0

Views: 430

Answers (1)

Kryptur
Kryptur

Reputation: 745

The Apache Web Server reacts to requests over the network. The RewriteRules are applied only to these requests.

The PHP require, include, file_get_contents, ... functions usually access the local storage instead of issuing a HTTP request to Apache over the network.

While using file_get_contents with URLs is possible and sometimes useful, in your use case you have to add the .php extension to address the correct local file.

Upvotes: 1

Related Questions