Forum Moderators: phranque
/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
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
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
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.
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]
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
RewriteRule ^(([^/]+/)*)(([^.]+\.)+(js¦css))$ $1abc.php?filepath=$3 [L]
Jim