N
N
nikolayv2014-04-04 20:15:30
PHP
nikolayv, 2014-04-04 20:15:30

Why is a redirect set in .htaccess, but the script fires multiple times?

It is necessary that in the subfolder all requests go to index.php. The structure of requests is planned as follows: site.ru/cabinet/show/post/
There is a folder on the site: site.ru/cabinet/. It contains .htaccess:

RewriteEngine on
RewriteBase /cabinet/

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule  ^([^.]+[^./])$  $1/  [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -l
RewriteCond %{REQUEST_FILENAME} -d 

RewriteRule ^.*$ [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Index.php is like this:
<html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
  <title>Проверка</title>
  <link rel="stylesheet" type="text/css" href="files/screen.css">
  <link rel="stylesheet" type="text/css" href="files/main.css"> 
</head>
<body>
<?
  $mailmes="Проверка";
  $send_to = "[email protected]";
  $subject = "Проверка";
  $headers = "From: cabinet <[email protected]> \n";
  $headers .= "Content-type: text/html; charset=UTF8\r\n";
  $send = mail($send_to, $subject, $mailmes, $headers);
?>
</body>
</html>

The problem is that with every request on the page, a script is processed. For example, in the index.php above, the email is sent 3 times. Once per request for the page itself, and two more times for including two CSS files.
I understand that this comes from .htaccess, and specifically because of the lines:
RewriteRule ^.*$ [NC,L]
RewriteRule ^.*$ index.php [NC,L]

But if you remove the first line, then the script cannot process such a request: site.ru/cabinet/show/, a 404 page immediately appears. With the current .htaccess configuration, everything works fine, except for repeated script processing.
Question. How to properly configure .htaccess so that the script is executed only once per page start, as it should be?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
S
svd71, 2014-04-04
@nikolayv

The point is that you are redirecting ALL requests to index.php. In index.php itself, there are links to get css files. Do an analysis in index.php of what is requested and issue the correct file.
----
index.php must start with these lines:

<php
$url = $_SERVER['REQUEST_URI'];

if (strpos($url,  "/screen.css" > 0) {

//здесь может потребоваться вывод хэдера для css контента. смотрите на php.net
// тема про хэдер
echo file_get_content($_SERVER['DOCUMENT_ROOT']."files/screen.css");
die();
}

//точно так же повторяем для второго css файла
// для случаев таких файлов вывод прекращается по die()
// для index.php обработка идет дальше

N
Nazar Mokrinsky, 2014-04-05
@nazarpc

I think it will be useful for you to read once and thoroughly understand how mod_rewrite works:
habrahabr.ru/company/sprinthost/blog/129560
It also says about looping

D
daMage, 2014-04-04
@daMage

Refine htaccess:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

V
Vampiro, 2014-04-04
@Vampiro

and I would set the paths for css and js from the root of the server and not take out Moscow ....
link rel="stylesheet" type="text/css" href=" / css/screen.css">

Z
zooks, 2015-08-21
@zooks

In your case, it is better to use the search from Google or Yandex.
https://cse.google.com/
https://site.yandex.ru/

V
vista1x, 2015-08-21
@vista1x

Usually, the search is performed on data from the database, but you, apparently, do not have it.

E
Evgeny Konkin, 2015-08-23
@konnn

vista1x is correct. Search becomes on . SELECT * FROM table WHERE title LIKE % keyword%

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question