is this a reasonable hack to path for images, js, css files

New to mod_rewrite? This is a good place to start.

is this a reasonable hack to path for images, js, css files

Postby mophilly » Mon Jul 20, 2009 11:40 am

On a server, I have several sub-domains used for variants of a base web app. In the examples below, "mysubdomain" is one, and there are others like "yoursubdomain", "hersubdomain" and so on. Each of these sub-domains share a single Apache module used like a cgi script. There are two ways the mod is invoked, the difference being the input parameter values, e.g. "cgi_A" and "cgi_B".

In addition, the rewrite rules consider other sub directories used in each sub-domain. I added these because the first rules, 1 and 2 below, work fine but have the side effect of looping through redirect URLs that contain "/ws/css" and similar.

I have two questions about the rewrite rules below:

A. How to write a rule to eliminate the "/ws" URL element if not part of "/ws/cgi_A" or "/ws/cgi_B"? That is, "subdomain/ws/css/" ought to be "subdomain/css/"

B. The current version, below, gets close but not quite. The rewrite log contains the following, which looks right but the browser does not load the js file.
Code: Select all
internal redirect with /js/tablesorter/jquery-latest.js [INTERNAL REDIRECT]


B. In general, is there a nicer way to structure these rules?

TIA,

- Mophilly


Short "public" urls:
www.maindomain/cgi_A/C2456D78
www.maindomain/cgi_B/E2L00087F2495876

The real paths look like this:
Code: Select all
.../htdocs/mysubdomain
   /htdocs/mysubdomain/css
   /htdocs/mysubdomain/images
   /htdocs/mysubdomain/js


Here is the Apache directives I am using:
EDIT: I realized the original example was totally wrong. I replaced it with the current version, which also does not work as desired but is *a lot* cleaner and (I hope) closer to hitting the mark.

Code: Select all
<DirectoryMatch /mysubdomain>
  AllowOverride None
  RewriteEngine on
  # handle sub-directories
  RewriteRule ^.+(/css/.+)$ $1 [L]
  RewriteRule ^.+(/images/.+)$ $1 [L]
  RewriteRule ^.+(/js/.+)$ $1 [L]
    #
  ### check for short URLs to be handled by custom modules
  #
  # 1. check for "action" short URL
  RewriteCond %{REQUEST_URI} !^/mysubdomain/cgi_A$ [NC]
  RewriteRule /PendingAction/([0-9]+)$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Main&OmnisServer=5914&ActionId=$1 [R,L]
  #
  # 2. check for "document" of short URL
  RewriteCond %{REQUEST_URI} !^/mysubdomain/cgi_B$ [NC]
  RewriteRule /ws/([a-z0-9]+)$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Alternate&OmnisServer=5914&document=$1 [NC,L]
  #
</DirectoryMatch>
mophilly
 
Posts: 6
Joined: Mon Jul 20, 2009 11:02 am

Solved: is this a reasonable hack to path for images, etc.

Postby mophilly » Mon Jul 20, 2009 2:32 pm

I clearly went overboard on this. I found a workable solution in the following code. Still, I question whether listing the specific sub-directories in RewriteCond's is a good approach.

Code: Select all
<DirectoryMatch /mysubdomain>
  AllowOverride None
  RewriteEngine on
  # 1. check for "action" short URL
  RewriteCond %{REQUEST_URI} ^/mysubdomain/Action/$ [NC]
  RewriteRule /PendingAction/([0-9]+)$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Alt_1&OmnisServer=5914&ActionId=$1 [R,L]
  # 2. check for "document" of short URL
  RewriteCond %{REQUEST_URI} !^/mysubdomain/Action$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/css$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/images$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/js$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/webapp$ [NC]
  RewriteRule /dashboard/([a-z0-9]+)$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Alt_2&OmnisServer=5914&document=$1 [NC,L]
  #
</DirectoryMatch>
mophilly
 
Posts: 6
Joined: Mon Jul 20, 2009 11:02 am

Postby richardk » Mon Jul 20, 2009 4:54 pm

I don't fully understand.


Where's the sub domain?

Code: Select all
  # handle sub-directories
  RewriteRule ^.+(/css/.+)$ $1 [L]
  RewriteRule ^.+(/images/.+)$ $1 [L]
  RewriteRule ^.+(/js/.+)$ $1 [L]

The HTML <base> element is a better fix for relative paths. FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.

Code: Select all
  # 1. check for "action" short URL
  RewriteCond %{REQUEST_URI} ^/mysubdomain/Action/$ [NC]
  RewriteRule /PendingAction/([0-9]+)$

If the REQUEST_URI is /mysubdomain/Action/, it can't contain /PendingAction/. The rule set should never match, unless i'm missing something. Even if you meant to include a ! it would have the same problem as below.

Code: Select all
  # 2. check for "document" of short URL
  RewriteCond %{REQUEST_URI} !^/mysubdomain/Action$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/css$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/images$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/js$ [NC]
  RewriteCond %{REQUEST_URI} !^/mysubdomain/webapp$ [NC]
  RewriteRule /dashboard/([a-z0-9]+)$

Again, if the URL contains /dashboard/ it wouldn't match /mysubdomain/images (for example) so the conditions are redundant.

What is a full /PendingAction/ URL like?
What is a full /dashboard/ URL like?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mophilly » Tue Jul 21, 2009 7:47 am

Thank you for the reply.

"mysubdomain" is the sub domain. Perhaps I am using the wrong term for this. Physically, it is "htdocs/mysubdomain/".

I cannot use HTML <base> because the various sites share HTML template files in a manner similiar to ZOPE templates or Django apps. I did read the FAQ, which is a great thread and very helpful.

"PendingAction" is a typo. It should be simply "Action" in the post. Ditto for "Dashboard" and "mysubdomain". Sorry about that.

So, the basic site, "mysubdomain", has very few static html pages. Most of the user experience is data driven using templates stored on the server. The two terms "Action", aka "PendingAction", and "document" must be interpreted as calls to the common cgi (a mod.so really).

All other requests need to be treated in the normal fashion. That includes all the css, js, and image files, as well as a few static html pages.

When a request comes in, it may be a cgi request. These appear in two forms:
1. /mysubdomain/Action/8948756739848576568DD23043
2. /mysubdomain/CD304kdh123

The first is easy to catch, the second less so. The desire of the designer is to provide short URLs appropriate for email distribution. All other requests should be treaded in the normal fashion.

I hope this makes sense. I am very new to rewrite rules and the design feels kinda clunky. All advice to improve this is very welcome.

- mophilly
mophilly
 
Posts: 6
Joined: Mon Jul 20, 2009 11:02 am

Postby richardk » Tue Jul 21, 2009 12:22 pm

"mysubdomain" is the sub domain. Perhaps I am using the wrong term for this. Physically, it is "htdocs/mysubdomain/".

A sub domain is like subdomain.example.com. For example.com/something/file.html, something would be a folder, directory or sub directory.

All other requests need to be treated in the normal fashion. That includes all the css, js, and image files, as well as a few static html pages.

When a request comes in, it may be a cgi request. These appear in two forms:
1. /mysubdomain/Action/8948756739848576568DD23043
2. /mysubdomain/CD304kdh123

Then i think you could use
Code: Select all
options +FollowSymLinks

RewriteEngine On

RewriteRule ^.+(/(css|images|js)/.+)$ $1 [L]

RewriteRule ^Action/([0-9]+)/?$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Alt_1&OmnisServer=5914&ActionId=$1 [QSA,L]

# Do not match requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Do not match requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/?$ /mysubdomain/webapp?OmnisLibrary=APPSERV&OmnisClass=rt_Alt_1&OmnisServer=5914&ActionId=$1 [NC,QSA,L]


You could also replace
Code: Select all
# Do not match requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Do not match requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d

with
Code: Select all
RewriteCond %{REQUEST_URI} !^/mysubdomain/(Action|webapp)/?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mophilly » Sun Jul 26, 2009 10:23 am

Thank you for the very clear and detailed answer. Very enlightening.

I will try these out this week!
mophilly
 
Posts: 6
Joined: Mon Jul 20, 2009 11:02 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 24 guests

cron