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

URL rewrite with .htaccess make duplicate mysql entries(使用 .htaccess 重写 URL 生成重复的 mysql 条目)
本文介绍了使用 .htaccess 重写 URL 生成重复的 mysql 条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果使用 .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.

解决方案:

  1. 添加 favicon.ico 文件
  2. 如果请求的 url 是 favicon,则不要插入(在 index.php 中)
  3. 使用规则在您的 htaccess 中禁止它

这篇关于使用 .htaccess 重写 URL 生成重复的 mysql 条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)