views:

97

answers:

2

At the moment, I have a standard installation of Wordpress 3.0, and the .htaccess file looks like this:

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

I want to rewrite URLs so that I can provide some automatically generated pages, but keep it within the Wordpress theme. For example, going to /event/123/ should pull the details for event #123 from my database, without the page existing in Wordpress.

I was thinking I could create a "generic event" page (eg. /event/generic/) in Wordpress and have it take a parameter, such as:

RewriteRule ^event/(\d+)/$ /event/generic/?q=$1 [L]

However, that doesn't seem to work, it just takes me to the Wordpress 404 page. I'm thinking that I might be asking a lot of Wordpress here, but surely it's possible?

The only other solution I've thought of would be to hack 404.php within the theme.

Update

My .htaccess looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^eventtest/(\w+)/$ /events/event-single/ [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

If I then go to /eventtest/testing/, then:

$_SERVER['REQUEST_URI'] is /eventtest/testing/
$_SERVER['REDIRECT_URL'] is /events/event-single/

The /events/event-single/ page exists in Wordpress, but if I go to /eventtest/testing/, I get a 404 page.

A: 

That rule looks fine. But make sure that you put that rule in front of Wordpress’ catch-all rule so that a request gets rewritten before it is caught by Wordpress’ last rule. So try:

RewriteRule ^index\.php$ - [L]
RewriteRule ^event/(\d+)/$ /event/generic/?q=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Gumbo
I've tried that, and unfortunately it still goes to the Wordpress 404 page. The RewriteRule is OK, because sending it to test.php works just fine. I think the key point here is that I want to redirect to a page that doesn't exist within Wordpress, and 'fake' a page.For example, if I create a /test/ page in Wordpress, this results in a 404 page:RewriteRule ^event/(\d+)/$ /test/ [L]
Sam Starling
@Sam Starling: Does */event/generic/* exist at all?
Gumbo
@Gumbo - I've added an example above which should clarify things slightly.
Sam Starling
+1  A: 

There's no need to even touch your .htaccess - you can do everything you want within WordPress itself!

Check out the rewrite API and my answer on a similar topic.

TheDeadMedic
+1, this is why the WordPress rewrite rules have been simplified.
Adam Backstrom