【发布时间】:2015-09-14 16:22:39
【问题描述】:
这是我的 htaccess 文件,因为 Wordpress 提供了它
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
我在 /events 有一个页面,我想通过模板 PHP 文件添加一个文本块,具体取决于 URL 参数是否存在。
于是我上网查了一下,尝试为它写一个 .htaccess 规则。我想出的是这个
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^events/([^/\.]+)/?$ events/?flow=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
但是,我认为这并不像我希望的那样工作。原始页面的链接是 /events?flow=open-evenings 并且在事件页面中,我有通常的 $_GET 东西:
$flow = isset($_GET['flow']) ? $_GET['flow'] : '';
if($flow == 'open-evenings') { echo "blah"; }
但是当我尝试查看 URL /events/open-evenings 时,我得到了 404 错误页面。我是否将规则放在错误的位置,或者我没有做一些明显的事情?感谢所有帮助!
更新:这是使用 Wordpress,所以也许我不应该使用已经重写的 url?我应该改用 index.php 吗?但这仍然会获取我尝试使用的漂亮网址吗?
我已尝试将 URL 更改为 index.php,以尝试使其正常工作,但根据以下规则,我仍然得到一个 404 页面。
RewriteRule ^events/([^/]*)$ /index.php?flow=$1
任何人都可以帮助我尝试使其正常工作吗?
【问题讨论】:
-
[L]标志防止进一步的规则拾取重写的目标。 -
但它显示的页面未找到,这意味着我编写的规则不能正常工作,它在列表中是第一个。无论有没有
[L],都会发生同样的行为 -
No 404 表示没有与
./events匹配的目录/文件。所以大概这是一个虚拟请求路径,或者不是? -
这是 Wordpress 中的一个页面,所以我想它是虚拟的,是吗?
-
如果不是真正的php入口脚本,那就是虚拟的。包罗万象的人必须把它捡起来。
标签: php wordpress .htaccess mod-rewrite