WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I checked this post here Making a plugin file accessible via url rewrite? which seems to have the same problem as me but its for a plugin. But so far i am getting 404.

add_action( 'init', 'my_rewrite' );
function my_rewrite() {
    global $wp_rewrite;

    add_rewrite_rule('/$', '/wp-content/themes/econ/adserver/adserver.js.php', 'top');

I have this file path /wp-content/themes/econ/adserver/adserver.js.php and need to show it as http://mysite.com/adserver.js.php


The code that Bainternet gave is working good except the rewrite rule part. I can now access the file using index.php?myjs=true. Here is the code in my htaccess file you might wanna take a look.

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

# END WordPress
share|improve this question
using /$ is bad news, change to something like js/$ and your url to mysite.com/js/adserver.js.php – Bainternet Aug 23 '11 at 20:53
@Bainternet yes also tried /adserver$ to have something like mysite.com/adserver/adserver.js.php but did not worked either. – Sisir Aug 23 '11 at 21:00
If the resultant code is javascript, why not simply enqueue it as javascript, where appropriate? – Chip Bennett Aug 23 '11 at 21:05
@Chip Bennett : Some get variable will send via the php file that will change the javascript file and also the url will be called from external sites. It is an ad rotating script. – Sisir Aug 23 '11 at 21:10
@Sisir the first one shouldn't be a problem. A javascript <script> can be completely encapsulated by a PHP function some_script() {} call, allowing you to pass PHP variables to the script. – Chip Bennett Aug 24 '11 at 1:54
up vote 1 down vote accepted

Ok here is a test working solution:

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' );
share|improve this answer
what should i do with it? Paste it into functions.php? do i need to change the line include( dirname( __FILE__ ) . '/adserver.js.php' ); to include( dirname( __FILE__ ) . '/adserver/adserver.js.php' ); as that file is inside the directory adserver? – Sisir Aug 24 '11 at 11:27
ok. I have placed the file in the theme root directory. using you same code in the functions.php file and trying to access the file with the url http://mysite.com/adserver.js' still getting 404` error. But your part of the code is working check beta.econengine.com/index.php?myjs=true we can access the script like this. Still need the rewrite think working. :) – Sisir Aug 24 '11 at 11:57
The rewrite is working on my end, try flushing your rewrite rules, simply go to your settings>>permalinks and save. – Bainternet Aug 24 '11 at 15:05
Thanks! Working after flashing! Great help! :) – Sisir Aug 24 '11 at 15:51
interesting! it looks like wordpress functions are not defined in that php file? what gives? check this url beta.econengine.com/adserver.js.php?id=500 – Sisir Aug 24 '11 at 17:06

WP rewrite rules are not real rewrites nor will they allow redirects like that. So you can't use them that way. Fortunately, WP should have you covered there even if you try.

See, an internal WordPress rewrite rule must change a URL pattern into an index.php?key=value pattern.

So when you use add_rewrite_rule to insert a rule that doesn't point to the index.php file, then this should get detected and a "hard" rule will be created in the .htaccess file for it. Thus, check the .htaccess file after doing the hard flush and see if your new rule exists there as a normal RewriteRule. If not, then maybe the .htaccess isn't writable.

share|improve this answer

Try this:

add_action( 'init', 'my_rewrite' );
function my_rewrite() {
    global $wp_rewrite;

    add_rewrite_rule('^adserver.js.php$', '/wp-content/themes/econ/adserver/adserver.js.php', 'top');

And you should probably remove $wp_rewrite->flush_rules(true); once you have it working, since that's a big performance hit on every page view.

share|improve this answer
Sorry man! not working :( – Sisir Aug 23 '11 at 21:13
Are "pretty" permalinks working on your site? Can you paste the contents of your .htaccess file? – Devin Humbert Aug 23 '11 at 21:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.