我在这里查看了这篇帖子Making a plugin file accessible via url rewrite? 这似乎和我有同样的问题,但它是一个插件。但到目前为止,我得到了404。
add_action( \'init\', \'my_rewrite\' );
function my_rewrite() {
global $wp_rewrite;
add_rewrite_rule(\'/$\', \'/wp-content/themes/econ/adserver/adserver.js.php\', \'top\');
$wp_rewrite->flush_rules(true);
}
我有这个文件路径
/wp-content/themes/econ/adserver/adserver.js.php
需要显示为
http://mysite.com/adserver.js.php
UPDATE:
代码
Bainternet 除了重写规则部分,give工作正常。现在,我可以使用
index.php?myjs=true
. 这是我的htaccess文件中的代码,你可能想看看。
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
最合适的回答,由SO网友:Bainternet 整理而成
好的,这里有一个测试工作解决方案:
<?php
/*
Plugin Name: wpse26719
Plugin URI: http://en.bainternet.info
Description: Need to make a php file inside theme accessible via url
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
// Register a URL that will set this variable to true
add_action(\'generate_rewrite_rules\', \'wpse26719_rw\');
function wpse26719_rw($wp_rewrite) {
$newrules = array();
$new_rules[\'^adserver.js.php$\'] = \'index.php?myjs=true\';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
// Add myjs as a query var
add_action( \'query_vars\', \'wpse26719_query_vars\' );
function wpse26719_query_vars( $query_vars )
{
$query_vars[] = \'myjs\';
return $query_vars;
}
// If this is done, we can access it later
// This example checks very early in the process:
// if the variable is set, we include our page and stop execution after it
add_action( \'parse_request\', \'wpse26719_parse_request\' );
function wpse26719_parse_request( &$wp )
{
if ( array_key_exists( \'myjs\', $wp->query_vars ) ) {
include( dirname( __FILE__ ) . \'/adserver.js.php\' );
exit();
}
}