.htaccess: redirect to sub directory

timgavin

I've written a Wordpress plugin which creates .html files. I'm trying to get it so that when a visitor hits sitename.com/newsletters it shows the index.php file in /wp-content/plugins/my-plugin/files

Of course, the index.php file is a list of all the other files in the directory, so when someone hits sitename.com/newsletters/january.html the browser loads /wp-content/plugins/my-plugin/files/january.html, but the URL remains http://sitename.com/newsletters/january.html

I thought this should take care of it, but it's not working. Just gives me a "Post not found" error.

RewriteRule ^newsletters/(.*) /wp-content/plugins/my-plugin/files/$1 [L]

Here's my .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^newsletters/(.*) /wp-content/plugins/my-plugin/files/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
Olaf Dietsche

Your rule looks good and does what it is supposed to do.

But in an .htaccess file, when a URL is rewritten, the resulting URL is fed back into the rules once more.

There it skips RewriteRule ^newsletters/..., and comes to RewriteRule . /index.php. But index.php, the main entry point for Wordpress doesn't know about your files in /wp-content/plugins/my-plugin/files/.

To fix this, you must prevent index.php swallowing your rewrite. You can do this by prefixing your RewriteRule with

RewriteRule ^wp-content/plugins/my-plugin/files/ - [L]

which exits the rule chain without rewriting further.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related