开发者

Wordpress, .htaccess and mod_rewrite: how can I add redirection rules?

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.

Up开发者_如何学运维date

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.


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.


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜