How can I store my Rewrite Rules in a database? - mysql

Im developing a new site, and I'd like to store my rewrite rules in a database, instead of right in the .htaccess files.
I have another site that uses Opensef (http://sourceforge.net/projects/opensef/) with a Joomla! installation that is doing this, but im not even 100% how it works underneath the hood.
How can I store these rules in a database, query for them on request and rediret to the clean URL if found? Is there a better way to do this instead of loading up a .htaccess file (there may be 1000's of entries)?
Thank you,

You can get mod_rewrite to generate a map from external source such as executing a PHP or Python file which can get the data from the database and create a mod_rewrite map.
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
(See right at the bottom)
For example
RewriteMap quux-map prg:/path/to/map.quux.pl
Good Luck

Assuming all these pages are ultimately in Joomla, I think using .htaccess or mod_rewrite is a mistake.
I think you're much better off learning how Openserf works. I'd imagine it has a little piece of code that runs early on for every request that queries the database and issues a Redirect through PHP if there's a hit. A further advantage of this approach is that it should even be possible to have Joomla rewrite links on its pages to point to the clean version in the first place, saving the user an unneeded redirect
Incidentally, this is how the Pathauto module in Drupal does it, and I use that all the time on some pretty high volume sites with many thousands of pages.

I think that the best approach to use rules stored in a database is:
Store the rules in your database through your admin panel of your site.
Then after updating database, generate a new .htaccess using the rules in DB using your server-side language solution.
Replace old .htaccess with new one.
This avoids the server load. It's similar to Aiden Bell solution.

Grab the UrlRewriteFilter, butcher it to use a DB, and use that in Tomcat instead of Apache.
Tomcat is a fine web server and can do many things Apache can do (like FastCGI for PHP), and writing stuff like this for it is trivial compared to writing such things for Apache.

What you probably want is a single rewrite rule to handle every unknown request that comes in and then pass that to a small script that will handle the lookups & generate redirects. You could even skip the rewrite rule completely and use the Apache ErrorDocument directive to pass unknown URLS into the script.
You've been pretty slim on the details of what this 'new site' is but, you might want to consider building yourself a Front Controller for the app & having it take care of all the incoming URLs. Many (most?) web app frameworks take this approach.

Related

How to read from a database, add some of that information to html, and send that html to a user?

I'm designing a website where users can upload comments on pages, and other users should see those comments. I reached the stage where I have the comments stored in a database, and I know the place they're supposed to go in the html, and I need to connect those two things somehow.
I'm using express and Node.js on the server side, and postgres on the db side.
As of when I'm asking this, it seems to me it's very bad practice to have the user access the database. So I think the server needs to access the database based on the user's request, modify the generalized html's showing of comments to now have the information of the specific comments, save that to a file, and send it to the user. To do this I was thinking of creating an "html generator function" on the server-side that takes in specific comment information and puts it in the generalized html, but that seems like it doesn't scale well and I'm concerned that storing the intermediate file would be inefficient.
Is that the correct approach? Can you tell me known ways of doing this that aren't so hacky?
If you suggest using php, isn't there a problem where php connects to a server and disconnects every time we use it? I would prefer if the server connected once when it booted and did all the fetching when needed instead of connecting every time. It seems to me like that would involve far less overhead (correct me if I'm wrong...)
See the comment of Amadan for the full solution. It's called a "template engine"
Edit:
I highly recommend learning React. I learned EJS and it's difficult to scale. React is infinitely easier to program with for just a little more investment. The old web is much less declarative (& EJS is much less too).

Protect client side database like TaffyDB

I have to develop an application for smartphones using HTML/CSS/JS (for PhoneGap) and I have to store data somewhere.
After some research, I found TaffyDB (http://www.taffydb.com/) that exactly does the job except on one point : security.
I don't want someone to take all my data just by saving the JS file so is there a solution to protect it ?
Or if I want to keep my data private, do I have to use an usual database (like MySQL) coupled with a PHP script that I call via Ajax ?
Thanks for the help.
TaffyDB can be used on Server-Side with a number of server-side solutions, but you will have to control the output on your application to include just the data.
In general, unless you plan to use a javascript server-side solution, I would say you cannot make it "secure", and even if you use non sensitive data on your front-end, I would highly recommend you go through the OSWAP guide before writing any code to determine if it is secure or not.

How to modify and save html/css in server-side?

I'm new in this subject so this might be a silly question for most of you. I have a simple server which several users will access. If any of them change a CSS property of an element, the others should be able to see the change in real time.
Should I use something like node.js to perform this? How do I save the changes the users do?
The page would look something like this: http://stom89.dyndns.org/
Thanks!
I guess what you want to change in your CSS / html , are states. Like if a lamp is on/off? Then you need to save each state in a mySQL DB and just grab the data for each user. If you want it to look like realtime for online users, then use js(ajax) to sync data regularly.
Alternative way without a DB would be with files.
If you don't wanna use mysql for this, you can use files. I suggest using ini files. For more on how to read/write ini files, you can visit this question. It's super simple and you'll be able to have each variable in a nifty array.
What you need: A bit of PHP, a little bit of jQuery (or js), understanding of GET variables
I suggest you create 3 files.
index.php :
Your main page which is the client. Pulls info using get
variables. You can use jQuery.get() for this.
getstate.php :
This is the file which will read the ini file and give you back the states for each device. Read them with jQuery.get() from index.php .
savestate.php:
This is the file which you'll send the new states to from index.php Example request: http://address.goes.here/savestate.php?bedroomlight=1&garagelight=0
Whats even more interesting is that ini files can be written/read easily by many programming languages so you can manipulate the data using your Raspberry Pi easily. (say someone turns of a light, a script polling state could change the ini file)
I think you would need to use a sql database and have a javascript to detect changes and update through AJAX. That's my best idea.
I have been messing with this subject for sometime if I completely understand your question. I would suggest looking at python, ruby or node.js though I could not say which is the easiest to learn for you though I would suggest python and a comet server which could be ape and simply have the server push the updates to the users that are already on the site.
Edit:
Suggestions for polling :: jQuery
http://api.jquery.com/jQuery.get/ for standard data retrieval which is about all you will need.

Dealing with mass images, some small - some large, in spring/java application using mysql

I was wondering what the best pattern was to handle the management of images these days when using spring/java and mysql.
I have several options. Some of the
images are just small avatars for
the users. Is it fine to put these
directly into mysql? Or use the file
system?
For the larger images, is file
system pretty much the only option,
and then use mysql to store the
location on the file system?
Where is a good spot to put them on
a linux server? /var/files/images?
Since the files are hidden from the
war deployment directory, what is
the best way to stream them? Use
some kind of a file output stream as
the response body for an http
request?
Also, do I have to develop all of
the file management stuff myself,
like cleaning up unused files and
the like?
What about image security? Some images should not be accessed by everyone. I think I'd need to use a separate url with Spring security checking the current user for this.
I'd appreciate advice on all of these questions. Thanks.
You could use MySQL, and that would have the advantage of centralization and easy cleanup, but IMHO it's a waste of the database's resources if you plan to scale.
For data like images where everything is public, consider something like Amazon S3 which allows you to serve images directly from S3's web servers. If you plan to host everything yourself, just serve from a directory. Just remember to turn directory listings off :)

Configuring authentication and authorization in Apache2 with MySQL

I am trying to configure Apache2 so I can use MySQL for authenticating users to access certain pages. Also authorization needs to work so different groups can reach differen pages.
Now, I have googled a lot but can't find out how to do this. At least not for the configuration I am having. There doesn't seem to be any version of mod_auth_mysql that supports my configuration.
OSX 10.6.4
Apache 2.2
Now, how do I achieve this not creating my own login-application in php but using the built in support of Apache2? I'm totally stuck on this one...
Thanks in advance!
Regards,
Niklas
I also would like to use a taylormade login page, not pop-up
....
how do I achieve this not creating my own login-application in php but using the built in support of Apache2
Short answer is that you can't. Unless you rewrite the Apache source code or create your own module, you can't mix HTTP based authentication with non-pop-up login prompts.
However if you are using PHP then a relatively low impact solution would be to use auto-prepend to prefix every protected page with a check on the users session. Obviously this would need a bit more thought if it is to be applied to files other than PHP source files (it'll work perfectly well if you tell Apache to apply the php engine to, say .gif files, but your script will need to detect and return the appropriate mime type).
A simpler solution might be to put a tool like squid in front of the webserver as a reverse proxy, then make the session handling data available to the squid url-rewriter.
You can build mod_auth_mysql from the source. Have a look at this post.
I you want your own log-in page, you cannot use Apache to do authentication.
Apache authentication uses the Authentication portion of the HTTP standard, and as a result you're reliant on the browser to handle the interaction with the user. There are plugins (like mod_auth_mysql) to use a database as the backend for this, but Apache can't ask the user for their credentials any other way.
The fastest approach might well be to set up Drupal or Joomla, which will get you user and group management as well as full content editing, in a way that will let you control who can edit what.