Forum Moderators: phranque

Message Too Old, No Replies

Shared assets, executing PHP and rule ordering?

Have this almost entirely working, just need some clarifications please.

         

JAB Creations

6:08 am on Nov 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The setup is much simpler than it may seem: I have two (or more) sites...

http:// localhost/site1/
http:// localhost/site2/

They are very similar in how they are constructed so I wish to share the asset files between them. That means if I make a change to the asset files (e.g. JavaScript, CSS, images, etc) then those changes apply to all the sites.

For once I've actually have had success reading Apache's documentation in regards to the QSA flag.

So I was able to have Apache have files here...

http:// localhost/scripts/*

...accessed this way...

http:// localhost/site1/scripts/*
http:// localhost/site2/scripts/*

...or to be more specific

http:// localhost/scripts/index.js

...can be accessed at....
http:// localhost/site1/scripts/index.js

There are some issues though. I'm having difficulty having Apache allow PHP to execute in this manner and sometimes it'll ask me to download the JavaScript file if I request it directly. I don't think PHP is being executed.

Also except for the exceptions and the script directory rule (and there will likely be other directories I have the exact same rule for) I think the order of the rules is rewriting everything to the target file instead of when I specify the script* directory/files.

So here is the code I have now (only difference is the the extensions in the exception list has been reduced for posting)...

http:// localhost/.htaccess
AddType application/x-httpd-php5 .css .js

RewriteEngine on
RewriteRule .*\/scripts(.+) scripts$1 [QSA]
RewriteRule .*\/themes(.+) themes$1 [QSA]
RewriteRule !\.(css|xhtml|xml|zip)$ rewrite.php


So what I want is that unless a request goes to scripts* or themes* or has a matching extension that it be handled by http:// localhost/rewrite.php. I also need to have PHP execute in certain file types as well. I've rearranged the rules, have tried putting the JavaScript/PHP association in scripts/.htaccess separately etc. I'm either remarkably close or really not, help please?

- John

Pfui

6:59 pm on Nov 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This Perl person just wanted to toss in something completely different before people tackle the PHP/rewrite stuff, precisely because it's so simple. If you've got command line access:

Symlink your shared assets directory/directories.

I Symlink a search. subdomain's files to its www. counterpart because they share everything but the search form and the guts of the search operations. (I even Symlink the search. sub's .htaccess file. Works like a charm because both share error documents, dynamic robots.txt files, etc.)

Here's a handy reference (because Symlink details can be easy to forget:) [uw714doc.sco.com...]

JAB Creations

3:44 am on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pfui, from what I've encountered thus far it seems that I'll need access to the server configuration in addition to .htaccess, if so I can't do this. I have to use what is available via .htaccess as this needs to be mirrored on a live server where I do not have access to the server configuration. Whether it's possible or not I'm not sure though I like the idea and appreciate your input regardless of whether that is an avenue I can use or not.

- John

JAB Creations

1:25 pm on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've corrected part of the issue, AddType should be AddHandler! Ops! Browsers no longer prompt me to download JavaScript files however it's still not executing PHP before sending the file(s) to the client. I'm even more convinced it's a conflict between the handler and the rewrite and this is the only thing I've been working on literally all weekend.

- John

wilderness

1:37 pm on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is not correct

.*\/

Forward slashes do not require escaping in Apache.

JAB Creations

1:49 pm on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Turns out besides the AddType and AddHandler mix up I was also looking at a file that requires administrative permissions otherwise it outputs nothing (thanks to PHP)...which had me a bit confused. After fixing AddType and AddHandler and then looking at onload.js everything seems fine.

Thanks wilderness, it still works fine after that adjustment.

- John

AddHandler application/x-httpd-php .css .js
AddType text/javascript .js

RewriteEngine on
RewriteRule .*/scripts(.+) scripts$1 [QSA]
RewriteRule .*/themes(.+) themes$1 [QSA]
RewriteRule ^(|site1\/|site2\/|site3) - [L]
RewriteRule !\.(css|xhtml|xml|zip)$ rewrite.php

g1smd

7:45 pm on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



QSA is the default, so you do not need to specify it.

You need the L flag on every rule.

Don't use the .* grouping at the beginning of a RegEx pattern.

xhtml|xml simplifies to x(ht)?ml