I want a script to open .htaccess and add an entry when i add a page. For example: I add a page called /page/dog-food/innova-dog-food-review and the script adds the following line: 5 and 9 being ID numbers from database. RewriteRule ^page/(dog-food/|dog-food)/(innova-dog-food-review/|innova-dog-food-review)$ /index.php?page=5&id=9 How hard would this be to accomplish?
It should not be too hard to do directly. Are you one shared hosting though? If so you may not want to do it directly, as this opens your .htaccess files to be writable by the web server, and thus anyone else on the system (unless your host is using a suexec thing to run php in). There are workarounds, and/or if you are using a VPS or dedicated server it matters less.
I hadn't thougt about it before, but you might be able to use php's ftp functions to make your .htaccess writeable, make your changes, and move the permissions back. That would make it safer on a shared host.
Almost, but now you would need to encode that php, or you just put you pw in a readable file for all the users on the host. You need to have the file with the pw readable by you only or encoded. Actually a bit safer way to do it , is to have a suid exe out of the directory tree that the web pages come from and exec that to do the writing to the file. At least this does not have your pw in a file encode or not, and your pw is not being sent in clear text for the ftp login in. You could then at least limit the what write actions are allowed to what that exe supports. You could probably also use session ids passed through shmem to add another layer of protection.
To answer your original question - about pulling numbers out of a database with mod_rewrite, No you can't. Not with a typical implementation anyways. You can use a rewritemap - calling an external program to return the url or conversely just build a rewritemap yourself in a text file. The problem with using a database for a rewrite rule is in the timing of everything. Opening a text file is a relatively quick task. Opening up a database connection, querying for data on that connection, computing a result and then closing the connection on every visit to your site can get system intensive and if there is high traffic you may run into memory issues. If you are doing a redesign, a rewritemap using a straight text file or db hash is the way to go.
I ran accross this the other day booked maked it but have not downloaded it yet. "AuthClassed is a class, written in PHP, to implement a simple authentification scheme for database-oriented webpages. Using AuthClassed is as simple as putting a single code line at the top of the webpage you would like to protect." https://sourceforge.net/projects/authclassed/
You can write to the .htpass or .htaccess safely even if you're on shared hosting, as long as your php script is running as cgi as your user. The second method I use to have dynamic lookups defined by the url is to have all matches passed by mod_rewrite to a php file which looks up into the db to see what page to generate. It works like this: mod_rewrite detects match, sends to view.php with match as param view.php takes match and looks it up into db and generates associated page
Following up on my last post: Instead of: RewriteRule ^page/(dog-food/|dog-food)/(innova-dog-food-review/|innova-dog-food-review)$ /index.php?page=5&id=9 You have: RewriteRule ^page/(.+) /index.php?match=$1 Then you take match, look it up in the db, and generate the associated page. Don't forget to use [L] so mod_rewrite doesn't check more rules.