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…
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: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. This might work: