使用.htaccess重写URL会产生重复的mysql条目

时间:2022-07-07 00:17:58

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

如果使用.htaccess重写url,则使用php执行所有INSERT查询两次(不需要的重复)

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.

在一次index.php加载后,我在mysql中有两个相同的条目。当我删除.htaccess时一切正常,所以问题必定存在。 .htaccess中的重写定义来自Wordpress - 我喜欢它。

I try Medoo framework, but entries is still duplicate.

我尝试Medoo框架,但条目仍然是重复的。

So, any suggestion? :-)

那么,有什么建议吗? :-)

2 个解决方案

#1


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.

浏览器默认自动请求favicon.ico文件。但是你没有任何favicon.ico文件,所以它被重写(在你的htaccess中规则)。这就是你有重复执行的原因。

Solutions:

  1. Add a favicon.ico file
  2. 添加favicon.ico文件

  3. Don't INSERT (in index.php) if url requested is favicon
  4. 如果请求的url是favicon,请不要INSERT(在index.php中)

  5. Disallow it in your htaccess with a rule
  6. 在你的htaccess中禁止它有规则

#2


0  

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

此外,您可以在SQL表中设置主键或唯一键(例如,'title')。仅此一项就可以防止任何重复的条目

#1


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.

浏览器默认自动请求favicon.ico文件。但是你没有任何favicon.ico文件,所以它被重写(在你的htaccess中规则)。这就是你有重复执行的原因。

Solutions:

  1. Add a favicon.ico file
  2. 添加favicon.ico文件

  3. Don't INSERT (in index.php) if url requested is favicon
  4. 如果请求的url是favicon,请不要INSERT(在index.php中)

  5. Disallow it in your htaccess with a rule
  6. 在你的htaccess中禁止它有规则

#2


0  

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

此外,您可以在SQL表中设置主键或唯一键(例如,'title')。仅此一项就可以防止任何重复的条目