gpt4 book ai didi

php - 如何使用 htaccess 来做到这一点?

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:04 25 4
gpt4 key购买 nike

我被困在了这里,我正在绞尽脑汁想弄清楚为什么它不起作用!

这是想让url看起来像

 example.com/news/top/

来自

 example.com/index.php?view=news&task=top

但我需要这样“任务”可以是不同的东西

 example.com/index.php?view=news&task=newest
example.com/index.php?view=news&task=help
example.com/index.php?view=news&task=write
....

当前的 .htaccess 看起来像:

DirectoryIndex index.php
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2

如果我访问 www.example.com/news/-> 它只加载 index.php 但不通过 ?view=news

如果我访问 www.example.com/news/top/-> 它工作正常。

但我需要它们都工作,向 .htaccess 添加新行,例如:

RewriteRule ^news/ index.php?view=news

然后访问 www.example.com/news/工作正常,但是访问 www.example.com/news/top/将无法获得任务的值(value)。

请帮帮我!

提前谢谢你。

最佳答案

尝试将这些规则添加到文档根目录中的 htaccess 文件中:

RewriteEngine On

# Redirect access to index.php
RewriteCond %{THE_REQUEST} \ /index\.php\?view=([^&]+)&task=([^&\ ]+)
RewriteRule ^ /%1/%2/? [L,R=301]

# rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?view=$1&task=$2 [L]

第一个将像 /index.php?view=news&task=foo 这样的请求重定向到 /news/foo/。然后第二条规则在内部将请求重写为 /news/foo/ 回到 /index.php?view=news&task=foo

如果您所有的链接都已经看起来像/news/foo/,那么您就不需要第一条规则了。

关于php - 如何使用 htaccess 来做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235553/

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