简体   繁体   English

自定义WordPress重写返回404

[英]Custom WordPress Rewrite returns 404

I would like to add several custom WordPress rewrites to a directory that contains some special pages. 我想将几个自定义WordPress重写添加到包含一些特殊页面的目录中。 I have added a directory named custom , which contains all of the custom pages. 我添加了一个名为custom的目录,其中包含所有自定义页面。

I have added rewrite rules, but for some reason whenever I try to access them, I get a 404 error. 我添加了重写规则,但是由于某种原因,每当我尝试访问它们时,都会出现404错误。 Here is a copy of my .htaccess : 这是我的.htaccess的副本:

Options +FollowSymLinks

# BEGIN Custom
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~meddept/
RewriteRule ^contact-us/? /~meddept/custom/contact.php [QSA,L]
</IfModule>
# END Custom

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~meddept/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~meddept/index.php [L]
</IfModule>

# END WordPress

The problem comes when trying to access contact-us/ . 尝试访问contact-us/ Visiting /custom/contact.php also returns the same 404 error, even though the file definitely exists... 即使文件确实存在,访问/custom/contact.php也会返回相同的404错误...

RewriteRule: http://82.147.22.3/~meddept/contact-us/
Direct file: http://82.147.22.3/~meddept/custom/contact.php

Can anyone explain why the first rule (with L flag) is failing? 谁能解释为什么第一个规则(带有L标志)失败了? I've tried with the numerous WordPress functions as well, but receive the same result... 我也尝试了许多WordPress功能 ,但是得到了相同的结果...

The problem with the 404 header was being caused by WordPress. 404标头的问题是由WordPress引起的。 Since I was using WordPress to handle the outputting of the header and footer, and there was no WordPress page defined, WP returned (incorrectly) a 404. 由于我使用WordPress处理页眉和页脚的输出,并且没有定义WordPress页面,因此WP返回(错误地)了404。

I was able to circumvent this by setting up The Loop before handling my code. 处理我的代码之前,我可以通过设置The Loop来规避这一问题。 Essentially, I updated contact.php to appear as follows: 本质上,我将contact.php更新为如下所示:

<?php

require('../wp-blog-header.php');

// Fix WordPress false 404s:
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();

get_header();

echo 'this is my content';

get_footer();
?>

And now everything works (including the custom routes) as expected. 现在一切都按预期工作(包括自定义路线)。

I'm not 100% sure, but I think your problem is, that you rewrite the base and then access the rest. 我不确定100%,但是我认为您的问题是,您重写了基础,然后访问了其余的基础。 This might work: 这可能起作用:

# BEGIN Custom
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~meddept/
RewriteRule ^contact-us/? custom/contact.php [QSA,L]
</IfModule>
# END Custom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM