Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've learned just a slight bit with WordPress Rewrite API -- just enough to add another rewrite rule for a plugin. The plugin works if installed in the root, but I'm finding that if one installs WordPress in a subdir, then I need to do the following changes to the .htaccess file. I'd like to know how to automate these changes if possible. As well, there's a second problem. If one goes to the Permalinks Settings in WordPress, my custom changes get overwritten (except for my special rule that uses WP Rewrite API). So, I need something that uses WordPress Rewrite API so that they don't get overwritten.

Original .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysubdir/
RewriteRule ^index\.php$ - [L]
# my special WP Rewrite API line
RewriteRule ^special(.*)$ /mysubdir/wp-content/plugins/special/app/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysubdir/index.php [L]
</IfModule>
# END WordPress

Changes I Made To Make Plugin Work in a Subdir

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

#RewriteBase /mysubdir/

RewriteRule ^index\.php$ - [L]
# my special WP Rewrite API line
RewriteRule ^special(.*)$ /mysubdir/wp-content/plugins/special/app/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#RewriteRule . /mysubdir/index.php [L]

RewriteRule . index.php [L]
</IfModule>
# END WordPress
share|improve this question
    
Rewrite rules should work fine regardless of where the site is installed (subdir or not). How did you add your rewrite? Seems like your question should be "Why doesn't my custom rewrite work with WordPress in a subdirectory?" –  chrisguitarguy Nov 27 '12 at 14:03
    
@chrisguitarguy I'm doing secured content in my plugin. I'm intercepting /my-post (because it's been marked in my plugin as secured) and redirecting to /special/secured/my-post (not a physical path, but a logical rewrite one), where my plugin intercepts and shows it only to logged-in members. –  Volomike Nov 28 '12 at 0:03
add comment

2 Answers 2

If you don't want Wordpress to overwrite your changes in .htaccess file, then make sure that you write your changes outside of the Wordpress comment block, that is, either before or after this:

# BEGIN WordPress
...
# END WordPress

Think of this area as Wordpress' territory. Anything within this block, will be overwritten by Wordpress once it has to make any changes to .htaccess.

This is what you can do after the Wordpress comment block:

# BEGIN WordPress
...
# END WordPress

<IfModule mod_rewrite.c>
# Write your rewrite rules here.
</IfModule>
share|improve this answer
add comment

You can use add_rewrite_rule() to add rules to the .htaccess file. If you add these rules on init (or just prior to when the rules are flushed), then anytime the rules are flushed (i.e. when permalink settings are saved), those rules will be kept.

Note, typically a rewrite passed to add_query_arg is of the form:

'index.php?p=12&food=$matches[1]&variety=$matches[2]'

and WordPress handles it internally and does not add them to the .htaccess file. But if the first part is not index.php? then it is treated as an external rewrite rule and added to .htaccess. See source.

share|improve this answer
add comment

Your Answer

 
discard

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.