使用 htaccess 从 index.php 重定向到 another.php 文件

Posted

技术标签:

【中文标题】使用 htaccess 从 index.php 重定向到 another.php 文件【英文标题】:Redirect with htaccess from index.php to another.php file 【发布时间】:2021-12-11 10:21:49 【问题描述】:

我想创建漂亮的网址。但是,我遇到了.htaccess 的一些问题。例如,我有 url domain/some.php?f=query-string。 我想更改 domain/query-string(预期的 url)。是否可以通过.htaccess 更改/重定向。或者可能来自 php 文件本身。

这是我做的一些 htaccess sn-p,但我得到它空白/错误页面

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %HTTP_HOST ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    RewriteRule ^/([^/.]+)$ some.php?f=$1    [NC,L]
</IfModule>

感谢您的关注。

【问题讨论】:

【参考方案1】:
RewriteRule ^/([^/.]+)$ some.php?f=$1    [NC,L]

.htaccess 中,RewriteRule pattern 匹配的 URL-path 不以斜杠开头,所以上面的永远不会匹配,它什么也不做。这应该写成如下:

RewriteRule ^([^/.]+)$ some.php?f=$1 [L]

这里不需要NC 标志,因为正则表达式已经“不区分大小写”。

【讨论】:

感谢您的解释。但是,我在上面尝试了您的答案,但仍然出现错误(some.php 中的内容未显示)。 @djodev 您请求的实际 URL 是什么?您得到的确切“错误”是什么? some.php 中有什么内容? 说实话,因为你上面的回答,一切都很顺利。谢谢@MrWhite :) 老实说,由于您上面的回答,一切都很顺利。谢谢你。再次查看我的php文件后,发现if else语句有错误。

以上是关于使用 htaccess 从 index.php 重定向到 another.php 文件的主要内容,如果未能解决你的问题,请参考以下文章

Laravel SSL .htaccess 重定向到 index.php

htaccess 从 url 中删除 index.php

index.php 的 .htaccess 重定向正在破坏 404 页

htaccess 仅针对域根将 index.html 重定向到 index.php

htaccess - 不需要的重定向到 index.php

htaccess 将所有请求重定向到 index.php,子文件夹除外