Open Write in Location in a .htaccess file

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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Open Write in Location in a .htaccess file

Post by tecktalkcm0391 »

How/ what do I need to make php open up .htaccess and then add in some text right before the #end username urls in:
# Start Username URLs
Redirect /chris /viewprofile.php?user=chris
# End Username URLs

And say I want it to find the line that has chris in it and delete it or replace it how would I do that ?

I've looked at fopen and fclose, I know how to do those but I cant find how to do this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file_get_contents() + strpos(), possibly substr_replace() + fopen() + fwrite() + fclose().

However, it would seem that you could engineer your rewrite such that it didn't need to be modified when you wanted to add someone.

There have been a few rewrite related posts that involved detecting if the request points to an existing file, directory, or symbolic link. Failing those tests, you can then direct it to pass that request to some handling script. This script could look for a context where that request makes sense, for instance searching the username list or performing a site-wide search or some other behavior.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Well, first make sure you have the right access to the file, CHMOD sounds like it?

Imho, you shouldn't be able to access/edit .htaccess file from php, because, if anyone could edit any .htaccess located on the web server root, then they will be able to gain access anywhere.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Couldn't you just use something like..
RewriteRule ^([0-9a-z_]+)/?$ /viewprofile.php?user=$1 [NC,L]
-NSF
Post Reply