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

anubhava
  • 761,203
  • 64
  • 569
  • 643
BenM
  • 52,573
  • 26
  • 113
  • 168
  • As both a stackfellow and namefellow (http://english.stackexchange.com/questions/54502/is-there-a-word-for-someone-with-the-same-name), I agree with @benm, this is BS. – Ben Nov 11 '13 at 11:43
  • 1
    Have you sure checked ownership of entries here: custom/*.*? – jacouh Nov 11 '13 at 11:46
  • @jacouh Yes. Both user and group ownerships match WP root files... – BenM Nov 11 '13 at 11:47
  • I was going to suggest the same as @jacouh. Is custom RX and contact.php R. – Ben Nov 11 '13 at 11:47
  • What happens in `contact.php` ? are you loading WP codebase and displaying custom html in it? – Ben Nov 11 '13 at 11:51
  • @Ben exactly. `contact.php` simply has: `require('../wp-blog-header.php'); get_header(); get_footer();` – BenM Nov 11 '13 at 11:59
  • @BenM You need to `exit()` before wordpress takes over completely, otherwise 404.. – Ben Nov 11 '13 at 12:02
  • after `get_footer()` for instance.. – Ben Nov 11 '13 at 12:02
  • Sure, theses rules are in ~meddept/.htaccess, but not in ~meddept/custom/.htaccess ... – jacouh Nov 11 '13 at 12:03
  • @BenM There are better ways to do this though, for instance `page-$id.php` or `page-$slug.php` files in your template folder (see http://codex.wordpress.org/Template_Hierarchy) – Ben Nov 11 '13 at 12:05
  • @Ben adding `exit()` after `get_footer()` has no effect. @jacouh yes, the rules are `~meddept/.htaccess`. – BenM Nov 11 '13 at 12:06
  • @BenM. See my last comment and link, there's way better options to do what you're trying to do in Wordpress.. – Ben Nov 11 '13 at 12:06
  • @Ben I know there are other ways to achieve this, but the point remains that the rewrites *should* work... – BenM Nov 11 '13 at 12:07
  • I've visited http://82.147.22.3/~meddept/custom/contact.php, 404 error also. – jacouh Nov 11 '13 at 12:07
  • @jacouh Correct, please read my question. If you visit http://82.147.22.3/~meddept/custom/ you can clearly see the file exists... – BenM Nov 11 '13 at 12:07
  • @BenM You *can* eat soup with a fork, it's just way easier with a spoon.. – Ben Nov 11 '13 at 12:07
  • You are all OK, simply you send a soft 404 header using PHP ? – jacouh Nov 11 '13 at 12:09
  • @jacouh No, there is no 404 header being sent from the file. – BenM Nov 11 '13 at 12:09
  • @Ben - nice analogy. That having been said, I really think it's easier just for me to handle this with `.htaccess`. I need some complex functionality within `contact.php` (as an example), which isn't suited to insertion directly into a WP Template file, or indeed the template's `functions.php` file. There's no reason why the rewrite shouldn't work as far as I can tell... – BenM Nov 11 '13 at 12:11
  • @BenM You're making a custom php file that runs `require('../wp-blog-header.php'); get_header(); get_footer();` (quoting you ;) How is that different? – Ben Nov 11 '13 at 12:16
  • Haha, that's a proof-of-concept for now. There will be a **lot** more functionality when the route is working... – BenM Nov 11 '13 at 12:18
  • Sounds complicated, wish enlightenment comes to you soon ;) – Ben Nov 11 '13 at 12:20
  • 1
    Please try custom/contact.php: – jacouh Nov 11 '13 at 12:22
  • Adding a plain `echo` statement to `contact.php` no longer produces the 404. Is it possible to have WordPress ignore the permalink lookup? – BenM Nov 11 '13 at 12:28
  • It's because WordPress header , you should correct it easily... – jacouh Nov 11 '13 at 12:41

2 Answers2

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.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • +1 to your question & answer for posting a solution. I removed `.htaccess, mod_rewrite` tags since that is unrelated. – anubhava Nov 11 '13 at 12:49
  • Thanks for posting your solution.. I still think it's a silly way to do it though ;) – Ben Nov 11 '13 at 22:57
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
Peon
  • 7,902
  • 7
  • 59
  • 100
  • Thanks. I had already tried this, and it produces the same results. – BenM Nov 11 '13 at 11:19
  • What if you remove the custom rewrite and just add `RewriteRule ^contact-us/ /~meddept/custom/contact.php [L]` to the existing one ( right below index.php redirect ) ? – Peon Nov 11 '13 at 11:25
  • The same result. 404s on both counts. – BenM Nov 11 '13 at 11:29
  • Yes. The custom rewrites from WordPress work fine: http://82.147.22.3/~meddept/about-us/ – BenM Nov 11 '13 at 11:58
  • Did you try debugging: http://stackoverflow.com/questions/7738170/how-to-debug-htaccess-rewrite-script it? – Peon Nov 11 '13 at 12:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40955/discussion-between-dainis-abols-and-benm) – Peon Nov 11 '13 at 12:14