V
V
Viktor Volkov2018-11-02 21:00:39
htaccess
Viktor Volkov, 2018-11-02 21:00:39

How does .htaccess work?

In the address bar I enter draft.local/test.php . There is such .htaccess

RewriteEngine On
RewriteBase /

#RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !/test\.php
RewriteRule .* test.php

in test.php there is a code with a request to the database to add one record:
$host='localHost';
$user='root';
$pass='';
$dbName='testDB';
$table='site';

$link=mysqli_connect($host, $user, $pass, $dbName);
mysqli_query($link, "SET NAMES 'utf8'");

$query="INSERT INTO $table (title, date) VALUES ('now', NOW())";

With such a set of directives in .htaccess, the database entry is added twice!! Can you explain how the process works? why is it calling test.php twice?
the code does not make sense, I just understand how .htaccess works.
I think I told him nonsense:
RewriteCond says that if this is not test.php, then the rule below should not be executed, but it turns out that the page is executed twice.
if you use this set:
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
#RewriteCond %{REQUEST_URI} !/test\.php
RewriteRule .* test.php

then it works correctly, although, as I understand it, it should have closed, because the address does not change during iterations inside .htaccess ..
in general, I would be grateful if you would speed up the sitation))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2018-11-03
@VGVolkov

The main points when compiling mod_rewrite rules are well chewed in the article https://habr.com/company/sprinthost/blog/129560/
Many modern browsers do not require an explicit indication <link rel="icon" href="/someimage.ico" />
and try to find the icon in the root of the site themselves.

RewriteCond %{REQUEST_URI} !/test\.php
RewriteRule .* test.php

When accessed, /favicon.icotest.php will be called.
This can be easily verified by adding the simplest logging to the script.
$log = date('H:i:s ') . $_SERVER['REQUEST_URI'] . PHP_EOL;
file_put_contents('log.txt', $log, FILE_APPEND);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question