Forum Moderators: phranque

Message Too Old, No Replies

Entire Folder path in htaccess variable

.htaccess,folder name,variable,mod_rewrite

         

Rupin Chheda

5:43 am on Dec 24, 2008 (gmt 0)

10+ Year Member



Hi,
How can I extract the entire folder path,irrespective of how many levels it has(var/folder1/folder2 or var/folder1---->both can be possible) in a .htaccess variable.
Assuming that the file name would be

/folder1/folder2/folder3....../foldern/filename.php

how can the regex seperate out( /folder1/folder2/folder3....../foldern/) and (filename.php)

Any help would be much appreciated
Thanks
Rupin

g1smd

1:04 pm on Dec 24, 2008 (gmt 0)

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



Something like:

(([^/]+/)+)([a-z0-9]+\.php)

You'll need $1 and $3. Note that $1 will contain a trailing slash.

Maybe:

^(([^/]+/)+)([^.]+\.php)$
instead.

[edited by: g1smd at 1:43 pm (utc) on Dec. 24, 2008]

jdMorgan

1:26 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the pattern "^(([^/]+/)*)(([^.]+\.)+)(.+)$"
and requesting a URL-path such as /one/two/three/my.file.html
you get
$1 = folder path (one/two/three/)
$3 = filename (my.file.)
$5 = filetype (html)

Back-references $2 and $4 are not very useful, as they will contain only the last-matched folder-path-part and the last-matched-filename-part, respectively. In my example URL's case, $2 = "three/" and $4 = "file."

As you can see, the trailing slashes and periods are included in the matched strings. It is possible to get rid of them, but at the cost of making the regex even more complex. The pattern shown here is therefore meant only as an example, and it will likely need to be modified to suit your exact needs.

The first two subpatterns are examples of negative matching. That is, the first subpatterns says "Match one or more characters not a slash, followed by a slash, and as many of those sequences (zero or more) as you like."

The second subpattern says, "Match one or more characters not a period, followed by a period, and require one or more of those sequences."

Another way to look at these negative-match patterns is to read them as "Match until you find a slash, then stop," and "Match until you find a period, then stop."

This regex example may be more complex than it needs to be because it is 'universal' -- That is, it allows periods in directory names, and multiple periods in filenames. If you don't need to support those options, then the patterns could be simplified.

Jim

Rupin Chheda

4:31 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Thanks Jim and g1smd for your help.
Based on your recommendations,I have written a snippet that will go in my .htaccess file

RewriteEngine on
Option +FollowSymlinks
<FilesMatch "\.(css¦js)$">
RewriteRule ^(([^/]+/)*)/([^/]+)$ /$1/abc.php?filetype=$2
</FilesMatch>

The final outcome of this would be(I am expecting :)) is that the rule would only select css and js files and split their folder and filenames,and append those values as shown,create a new URL and redirect.
I tried to upload this on my server root,but I recieved a 500 error.Can you please advise on what the correcttion would be?

I would also like to inquire and ask you if you could suggest a trial and error tool to tune my regular expressions for changes in the future..is any such tool available,or I plug it in a php file and try it out?
Thanks
Rupin

g1smd

5:03 pm on Dec 24, 2008 (gmt 0)

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



Are you wanting a redirect or a rewrite? This seems a convoluted way of doing what you want.

You don't need the filesmatch container, as the filetype is PHP here. The .css and .js parts appear only in the URL. Filesmatch matches the names of files on the server, not extensions in the URL.

You can specify the .css and .js parts in the RewriteRule (see my second example above). Use a pipe symbol for "or".

You need to add [L] to the rule if it is a rewrite.

For a redirect you need to add the both the hostname and [R=301,L].

For testing, create a password-protected test subdomain.

jdMorgan

5:43 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need $3 in your rule (count left parenthesis to resolve nested parentheses to matched values).

If all you want is the filetype (.css or .js) as the "filetype=" value, then the rule can be made more specific. Doing so will also prevent the recursion you have now, which is rewriting abc.php to abc.php in an 'infinite' loop, and causing a server error.


RewriteRule RewriteRule ^(([^/]+/)*)([^.]+\.)+(js¦css)$ /$1abc.php?filetype=$5 [L]

Test this exactly as shown, except for changing the broken pipe "¦" character to a solid pipe before use. (Posting on this forum modifies the pipe characters). You had also introduced duplicate (consecutive) slashes into your rule posted above.

There are a million ways to code things like this; The proper solution depends on *exactly* what you want to do. Please show us several different example URL-paths that would be requested from your server, and show us what you want the resulting value of "filetype=" to be.

Jim

Rupin Chheda

5:48 am on Dec 25, 2008 (gmt 0)

10+ Year Member



I am truly appreciative of the level of courtesy that you have shown.

The final solution that worked for me is

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(([^/]+/)*)([^.]+\.)+(js¦css)$ $1abc.php?filepath=$3$4 [L]

You guys are heroes!
Thanks a ton!

jdMorgan

5:55 am on Dec 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, if you want the entire filepath, then use:

RewriteRule ^(([^/]+/)*)(([^.]+\.)+(js¦css))$ $1abc.php?filepath=$3 [L]

That will capture the entire filepath in $3, and pick up all parts of the filename, even if it has several periods in it. Note the added level of parentheses around the whole filename+filetype.

Jim