Forum Moderators: phranque

Message Too Old, No Replies

Help needed adding to Wordpress REWRITE rule

         

jasonkthomas

4:40 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



I have the following rewrite rule in Wordpress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This is so that all posts can be viewed as "http://mydomain.com/name-of-post"

I want to be able to add another rewrite rule that loads a PHP file if someone goes to [mydomain.com...] So, I tried adding this right after the RewriteBase /:

RewriteRule ^content/(.+)$ /lib/content.php?desc=$1

I get a 404 error when doing this. Any ideas how can I get both to cooperate?

Thanks

jdMorgan

10:02 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at your server error log file, and see whether it is the originally-requested URL or the rewritten (/lib/content.php script) URL that is 404ing.

Jim

jasonkthomas

11:02 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



It's WordPress that's giving the error. It seems to be overriding / ignoring my rewrite rule.

jasonkthomas

12:22 am on Nov 6, 2007 (gmt 0)

10+ Year Member



I think what I need is a conditional rule that will check to see if it is being asked for /content and then do the re-write and then terminate reading of the htaccess file. If the condition doesn't match, continue reading the htaccess file.

I was thinking something like this:

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^/content/(.*)$
RewriteRule ^content/(.+)$ /lib/content.php?desc=$1
</IfModule>

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

# END WordPress

My conditional syntax doesn't seem to work. Can someone help me with this?

jdMorgan

12:32 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can dump all the ifmodule stuff. It's intended to prevent the server from flagging an error if mod_rewrite isn't installed, and as such creates a silent failure.

The additional RewriteCond is redundant, since the new RewriteRule requires the URL-path to start with /content.

As long as the /content rule precedes the WP rule and has an [L] flag, this code should work.

Jim

jdMorgan

12:35 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To clarify by example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#
RewriteRule ^content/(.+)$ /lib/content.php?desc=$1 [L]
#
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>

Jim

jasonkthomas

12:44 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Ok, here's how it looks now. However, I get an internal server error:

RewriteEngine On
RewriteRule ^content/(.+)$ /lib/content.php?desc=$1 [L]

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
# END WordPress

If I get rid of the Wordpress code, it works fine. So it doesn't like the both together for some reason.

Thanks

jasonkthomas

12:46 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Looks like you beat me to my post. Your code works. Thanks a lot!