URL rewrite with .htaccess make duplicate mysql entries

℡╲_俬逩灬. 提交于 2019-12-18 06:16:27

问题


If rewrite url with .htaccess, all INSERT query with php is performed twice (unwanted duplication)

My .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

And index.php:

<?php define('DB_LOGIN', 'mylogin');
define('DB_PASS', 'mypass');
define('DB_HOST', 'localhost');
define('DB_TYPE', 'mysql');
define('DB_NAME', 'dbname');

$mysql = MySQL_Connect(DB_HOST, DB_LOGIN, DB_PASS);
$mysql_db = MySQL_Select_DB(DB_NAME);

mysql_query("INSERT INTO `pages` (`title`, `slug`) VALUES ('TEST', 'test')"); ?>

After one load of index.php, I have two same entries in mysql. All is OK when I remove .htaccess, so, problem must be there. The rewrite definition in .htaccess is taken from Wordpress - i like it.

I try Medoo framework, but entries is still duplicate.

So, any suggestion? :-)


回答1:


Browsers automatically request the favicon.ico file by default.
But you don't have any favicon.ico file so it is rewritten (rule in your htaccess).
This is why you have a duplicate execute.

Solutions:

  1. Add a favicon.ico file
  2. Don't INSERT (in index.php) if url requested is favicon
  3. Disallow it in your htaccess with a rule



回答2:


Additionally you can set a primary or unique key in your SQL table (for example, 'title'). That alone would prevent any duplicate entries.



来源:https://stackoverflow.com/questions/21182407/url-rewrite-with-htaccess-make-duplicate-mysql-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!