Forum Moderators: phranque

Message Too Old, No Replies

include code with .htaccess without modifying page code at all

         

ramanguptaweb

6:10 am on Mar 24, 2005 (gmt 0)

10+ Year Member



any way to include a code or file on all pages with .htaccess without making any changes to the code or extension of pages by adding some code in .htaccess

sitz

1:41 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



There are potentially a couple of ways to handle this, although the first question is "why are you trying to do this at the server level; why not at the application level?"

That said:

1) Use the AddHandler and Action directives; I've not tried in this .htaccess before, but I have used it in httpd.conf; provided the FileInfo option has been set for AllowOverride, this should work:


#
# set a handler for the file extensions you want this
# to apply to
AddHandler my_action .html .htm .php

#
# define the action to take for that handler
Action my_action /URI/to/myscript.php

The PHP script could just as easily be a CGI script (or possibly even an .shtml file); it just needs to be able to include the file you care about (php could 'require' or 'include' it, a CGI could take some other action). Just be sure that the script defined in 'action' doesn't have one of the file extensions you used in your AddHandler line, or Apache will loop internally.

2) use mod_rewrite
To affect all files:


RewriteEngine on
RewriteRule ^(.*) /path/to/targetscript.php?srcpage=%1 [L]

To affect all htm and html files:


RewriteEngine on
RewriteRule ^(.*)\.html? /path/to/targetscript.php?srcpage=%1 [L]

And, again, have targetscript.php do whatever processing needs to be done and include/require the original page (stored in the 'srcpage' element of the query string).

This is off the top of my head; these solutions don't take into account any query strings being passed to the original file; you may have to experiment a bit if you need to handle that eventuality. Good luck. =)

charlier

1:59 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



if you are using php and I understand what you want, it sounds like you could also use the php pre_pend mechanism. In that case you want to have something like:

php_value auto_prepend_file /path/to/file/php_pre.inc

You can also inlcude one at the end of the page load with

php_value auto_append_file /path/to/file/php_post.inc

If you want a file in the middle of the page then you need to include and include directive in the page itself.

Of course this will only work for pages that are parsed by php

ramanguptaweb

8:29 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



Ok, let me tell you what I exactly wanna do. Actually I want to offer free web hosting. the pages that user will host can be PHP or just plain html. I want a way to include a code for my advertisement in all pages. All I can think is that this is possible through .htaccess only... Thanks for your response

sitz

11:29 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



1) if you have write access to httpd.conf, I'd do you configurations there. I freely admit I have a pet peeve against .htaccess files, but I know that for some people they're the only option. I have two main reasons for this peeve:

a) performance. Enabling AllowOverride for certain paths will result in a performance hit. I know that performance doesn't concern a lot of folks as much as it does me; I deal with a large amount of traffic in my day job. However, performance is more than just getting the user the data faster; if you can do that, you have a large capacity on existing hardware, which saves money.

b) Centralization. As soon as you start scattering .htaccess files all over the filesystem, that's one more thing that needs documentation (and we all *LOVE* writing documentation, don't we? =) ). Keeping all the configuration directives in one file (or, in the case of complex configs, a series of Include'd files, all in the same directory) makes everyone's life a bit easier.

All that said, what you basically want to do is prepend (or append) an HTML snippet (which may or may not include javascript) to all outgoing content. This can be done; the method I outlined above is one way to handle it. One could also make use of mod_perl, either by writing a small mod_perl handler which did what you needed, or by making use of HTML::Mason ([masonhq.com ]).

The thing to keep in mind is that Apache is a *webserver*, not a *content manager*; I'm not aware of directive that says "send this little file down the wire in addition to the reqeuested file every time a file of a certain type is requested" (although I'd be interested if there were). It's not (natively) designed to be able to manipulate the content it's returning. It provides a server-side includes mechanism which gives developers some additional abilities, and can be expanded to do pretty much anything you want, provided you're willing to expend the dev cycles to do it.

sitz

2:48 am on Mar 25, 2005 (gmt 0)

10+ Year Member



charlier: ooo, good to know that. Thanks. =)

(he says, one post and an hour or two late. This is what I get for having this thick skull; took this long to sink in. Go me. =) )

ramanguptaweb

9:10 am on Mar 25, 2005 (gmt 0)

10+ Year Member



I cant understan what you want me to do. please tell me how is that possible and if possible, kindly tell me. As i have a very little knowledge about .htaccess. I dont know whether I have rights to write httpocs.conf file that u r refering to. please suggest me what to do. Any help will be appreciated...