首页 > 解决方案 > 使用 htaccess 从 index.php 重定向到 another.php 文件

问题描述

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

这是我制作的一些 htaccess 片段,但我得到它空白/错误页面

<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>

感谢您的关注。

标签: php.htaccessredirectmod-rewritefriendly-url

解决方案


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

在中,模式.htaccess匹配的 URL 路径不以斜杠开头,所以上面的内容永远不会匹配,它什么也不做。这应该写成如下:RewriteRule

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

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


推荐阅读