拥有wordpress中特殊网址的重写规则

时间:2012-02-21 15:46:00

标签: wordpress .htaccess rewrite http-status-code-404

我有一个包含一些用户生成内容的数据库表。每个content-element都可以通过/ my-url / project / the-projects-name获得。这些项目都没有wordpress帖子。我创建了一个索引脚本,在调用这样的URL时我会重定向到该脚本。然后,该脚本提取项目名称,将其放入变量并包含页面模板。 我的页面和项目数据显示得很好,但是wordpress会为所请求的网站抛出404状态,并将标题设置为“Not Found!”。我如何伪造wordpress输出??

我的htaccess重定向到我的index-projects.php-script:

RewriteCond %{REQUEST_URI} ^/my-url/project/(.*)
RewriteRule . /index-project.php [L]

我的index-project.php:

ob_start();

define('WP_USE_THEMES', true);

$path = explode('/', $_SERVER['REQUEST_URI']);

while(empty($path[0]))
    array_shift($path);

while(empty($path[count($path)-1]))
    array_pop ($path);

if(count($path) >= 3 && !empty($path[2])) {

$postFound = false;                             // use this param to define if   requested post was found in DB
$post_name = $path[2];                          // get the post name from url

$args = parse_url($post_name);

$slug = $args['path'];   // extract the post slug from post name

if(!empty($slug)) {
    $wp_did_header = true;

    $_GET['page_id'] = SOME_ID_OF_MY_WP_PAGE;
    $_GET['main_page'] = 'page-project';
    require_once( dirname(__FILE__) . '/wp-load.php' );
    wp();

    include('./wp-content/themes/my-theme/page-project.php');
  } 
}

2 个答案:

答案 0 :(得分:0)

RewriteRule ^/my-url/project/(.*) /index-project.php [L]

RewriteRule ^/index-project.php - [L]

并确保在htaccess

中介绍WP规则

答案 1 :(得分:0)

Wordpress仍然会抛出404.页面正确加载,内容被“重定向”到我的index-project.php,但WP仍然将404标题返​​回给浏览器:

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