大网数据广告活动
网站运营与故障问答

Why rewrite to index.php is not working on a wordpress site?

· 52CE 我爱测

So my rewriterule is like RewriteRule ^test$ index.php [L] I tested it on https://htaccess.madewithlove.com?share=b0a9904b-1ec4-49cd-917c-36426ff6150a it works fine. I also tested it on live Joomla sites and on a HTML sites, it a…

问题原文

原帖链接 · Wen Shenk · CC BY-SA 4.0

So my rewriterule is like

RewriteRule ^test$ index.php [L]

I tested it on https://htaccess.madewithlove.com?share=b0a9904b-1ec4-49cd-917c-36426ff6150a it works fine.

I also tested it on live Joomla sites and on a HTML sites, it all work fine.

However, when I try to test this on a Wordpress site, I am always getting a 404 page. By default, wordpress site's .htaccess file is like

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I add my rewrite rule above to the first line, but when I open mysite.com/test I will get a 404 error.

To test, I create a 1.php file and modify my rule to

RewriteRule ^test$ 1.php [L]

This time, when I open mysite.com/test it successfully executes 1.php. So the catch here is index.php.

What I don't understand is if I open mysite.com/index.php it will load mysite.com without problem. And I believe

RewriteRule ^test$ index.php [L]

in essential is telling the server when user request mysite.com/test show him mysite.com/index.php, then why in actuality I am getting a 404 page(not a blank 404, but the 404 page handled by wordpress's theme) when trying to open mysite.com/test?

P.S. I've also tried removing the default wordpress rules, but still getting the same result.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

I am getting a 404 page(not a blank 404, but the 404 page handled by wordpress's theme)

Your rule is successfully rewriting the request to index.php, however...

index.php is the WordPress front-controller. Code in this file looks at the requested URL (eg. /test in your example) and decides how to handle it. If /test is not a valid route defined in WordPress then WordPress (not Apache) generates a 404. This isn't reporting that the file index.php does not exist, but that the URL /test does not exist inside of WordPress, ie. there is no route defined for this URL in WordPress.

I add my rewrite rule above to the first line

UPDATE: Incidentally, unless /test maps to an existing directory (or file) then the WordPress code block already does exactly what you are doing with the rewrite... it rewrites the request to /index.php with the last rule.

So, the directive you are adding is in fact entirely superfluous anyway.

RewriteRule ^test$ 1.php [L]

If the code in 1.php responds with a 404 (for whatever reason), then this would also result in a 404.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-02-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。