gpt4 book ai didi

php - .htaccess 未从网址中删除 .php 扩展名

转载 作者:行者123 更新时间:2023-11-29 20:02:07 27 4
gpt4 key购买 nike

首先我要说的是,我发现了很多脚本可以放入 .htaccess 文件中,并且我记下了所有这些脚本,这些脚本完全按照我想要的方式工作。

例如,如果我的 htdocs 文件夹中有一个名为 test.php 的文件,并且我转到 localhost/test.php,我希望该文件在浏览器中被重写为 localhost/test。

我发现了 2 个 .htaccess 文件可以做到这一点,我在下面展示了它们,但它们的问题是每当我尝试使用表单将数据插入 MYSQL 数据库时我总是得到空白字段。

这是第一个

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

这是第二个

# Turn on the rewrite engine
RewriteEngine on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

然后,在搜索了几个小时后,我发现了一些脚本,它们的工作原理就像魅力一样,然后昨天我去洗澡,回来后发现它们不起作用。 (我删除了旧的 .htaccess 文件,创建了一个新文件,重新启动了 mysql 和 apache,清除了浏览器数据等,但 .htaccess 仍然无法工作)并且在我去淋浴之前它们按照我想要的方式工作。

这两个之前工作过(不,我没有将它们同时放入 .htaccess 文件中)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

第二个

# Options is required by Many Hosting
Options +MultiViews

RewriteEngine on

# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

所以现在只有当我手动从 url 中删除 .php 时它们才起作用,但我不希望这样做,或者我不想手动更改 php 文档中的所有链接,使其不再具有 .php我想使用它们中的任何一个,因为当我使用表单将数据插入 MYSQL 时,数据实际上显示出来并且字段不为空。

我还发现一个添加了尾部斜杠的内容,因此 localhost/test.php 变成了 localhost/test/但它曾经工作过一次,现在它不再工作了,但是如果我使用表单将数据插入到MYSQL 数据库的字段也是空白的。

# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php

# End of Apache Rewrite Rules

就像我说的,我发现并记下了那些完全按预期工作的方法,但现在它们不起作用,有人知道为什么吗?或者如何调整它们以实现我想要的效果?我实际上花了过去一天的时间来寻找修复方法。顺便说一句,我在 Windows 7 上使用 XAMPP

最佳答案

重写时,发送的 URL 不是用于调用脚本的 URL,但您仍然可以访问原始脚本。在 index.php 中,在 ? 上分解 $_SERVER['REQUEST_URI'],第一项是您的路径,第二项是您的路径(如果存在) parse_str($exploded[1], $_GET);

关于php - .htaccess 未从网址中删除 .php 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com