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');
$wp_rewrite->flush_rules(true);
}
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
UPDATE:
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]
</IfModule>
# END WordPress
Ok here is a test working solution:
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.
Try this:
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.