Carlo
Carlo

Reputation: 39

What do these htaccess directives mean?

Can you help me to understand this .htaccess code?

RewriteEngine On 
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

I don't understand if is correct, but if I write my website url nothing happens....blank page. thank you

If you want I can add other informations

Upvotes: 0

Views: 64

Answers (1)

MrWhite
MrWhite

Reputation: 46012

In short, these directives internally rewrite all requests that do not map to physical files or directories or that end in .php (whether they exist as files or not) to /index.php in the document root.

There's nothing actually "wrong" with these directives. But whether they are "correct" depends entirely on what you are trying to do.

It is a "standard" front-controller pattern.

However, there is one thing... these directives are dependent on DirectoryIndex index.php being set in a parent config. This would normally be set in the main server config, although the Apache default (if this directive is not explicitly set) is index.html, not index.php. If this is not set to index.php then /index.php would not be called when requesting the document root. Although in this case you would expect a 403 Forbidden response, not a "blank page".

A "blank page" more likely suggests an error in your applciation.

RewriteRule ^(.*\.php)$ $1 [L]

This rule prevents any request that ends in .php from being rewritten/routed to /index.php. I said in comments that this is a little "misplaced", because if you are going to do something like this it would be far more efficient/simpler to combine it with the first rule and avoid the unnecessary filesystem checks.

For example:

DirectoryIndex index.php

RewriteEngine On 
RewriteBase /
RewriteRule \.php$ - [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule . index.php [L]

As it stands, the RewriteBase directive is not actually required.

But to clarify, this change makes no difference to the outcome of these directives (apart from maybe the addition of the DirectoryIndex directive). It just makes it more efficient/tidy.

Upvotes: 1

Related Questions