need to add/replace some content in a .htacces (wordpress)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ext103
Forum Newbie
Posts: 3
Joined: Wed Nov 24, 2010 6:16 am

need to add/replace some content in a .htacces (wordpress)

Post by ext103 »

hello all, new to the forum here and i hope you can help me.

i am working on a plugin that among other things writes some htaccess rules to the htaccess file upon activation.

the code i have now looks like this:

Code: Select all

register_activation_hook(__FILE__,'add_htaccess_rules_qss');
function add_htaccess_rules_qss(){
$file = $_SERVER['DOCUMENT_ROOT'] .'/.htaccess';
	if (file_exists($file)) {
		if (is_writable($file)) {
$fp = fopen($file, 'w');
fwrite($fp, '# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#START QUICK SITE SETUP HTACCESS RULES

rules that i have setup

#END QUICK SITE SETUP HTACCESS RULES
');
fclose($fp);
}}
}

this works fine, but there are a few problems. becuase i am new to php the way i have this working is to completly erase the file and start from scratch.

this removes the server specific wordpress rules, some wordpress htacces rules are different for different people/permalink structures.

i have looked at how wordpress searches for the #begin wordpress and #end wordpress to see how they just update the file but have had no luck.


what i really want to do is set the function up so it effectivley says:


if the htacces file has this in it "#start quick site setup htaccess rules" and "#end quick site setup htaccess rules" replace the contents in between these tags with "whatever i want to add." if it doesnt have those tags add "content"


does that make sense???


if i can manage this the plugin will not effect the wordpress rules, and will not duplicate the content at the end of the file.

up until now i have had to either replace the entire file, or add content to the end of the file (through 'w' and 'a') but neither is really a good solution.

i belive i need to look at preg_replace or strpos but cannot find a code snippet to learn from on the net

please help??
Post Reply