2

I would like to add several custom WordPress rewrites to a directory that contains some special pages. I have added a directory named custom, which contains all of the custom pages.

I have added rewrite rules, but for some reason whenever I try to access them, I get a 404 error. Here is a copy of my .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/. Visiting /custom/contact.php also returns the same 404 error, even though the file definitely exists...

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? I've tried with the numerous WordPress functions as well, but receive the same result...

4

2 回答 2

1

The problem with the 404 header was being caused by 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.

I was able to circumvent this by setting up The Loop before handling my code. Essentially, I updated contact.php to appear as follows:

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

于 2013-11-11T12:32:36.820 回答
0

I'm not 100% sure, but I think your problem is, that you rewrite the base and then access the rest. This might work:

# BEGIN Custom
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~meddept/
RewriteRule ^contact-us/? custom/contact.php [QSA,L]
</IfModule>
# END Custom
于 2013-11-11T11:16:02.827 回答