問題描述
如果使用 .htaccess 重寫 url,所有使用 php 的 INSERT 查詢都會執行兩次(不需要的重復)
If rewrite url with .htaccess, all INSERT query with php is performed twice (unwanted duplication)
我的.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>
和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')"); ?>
加載一次 index.php 后,我在 mysql 中有兩個相同的條目.當我刪除 .htaccess 時一切正常,所以,問題一定存在..htaccess 中的重寫定義取自 Wordpress - 我喜歡它.
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.
我嘗試了 Medoo 框架,但條目仍然重復.
I try Medoo framework, but entries is still duplicate.
那么,有什么建議嗎?:-)
So, any suggestion? :-)
推薦答案
默認情況下,瀏覽器會自動請求 favicon.ico 文件.
但是您沒有任何 favicon.ico 文件,因此它被重寫(htaccess 中的規則).
這就是您有重復執行的原因.
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 文件
- 如果請求的 url 是 favicon,則不要插入(在 index.php 中)
- 使用規則在您的 htaccess 中禁止它
這篇關于使用 .htaccess 重寫 URL 生成重復的 mysql 條目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!