Forum Moderators: phranque
I currently use the below lines in an htaccess file in every subfolder of a web site to grab and rewrite the request passing it over to a processing page.
RewriteEngine on
RewriteRule \.(html¦htm¦php¦php4¦php5)$ /jfk/jfk2/proc.php?url=%{REQUEST_URI}&%{QUERY_STRING} [R]
I built a template system. The above rewritrule grabs the request and sends it to my proc.php file where I parse out the original path/filename so the values can be used to setup the dynamic navigation controls in the page then the page is redirected to my index.php passing the original info and any parameters into the page.
Index.php loads my template, sets up the navigation based on any passed in parameters and then loads the requested content page in the content window section of my template.
JFK is the root folder of the site and JFK2 is the root folder of the new version I am working on. Eventually the content of JFK2 will be moved to the JFK folder.
Currently I have to place a copy of the same HTACCESS file in every folder below the JFK2 level for the template to work. I am trying to figure out how to put a single copy in the JFK2 folder that would work for all sub folders. I have to figure out how to exclude any files in the root folder from the redirection though so I do not end up in a loop.
Is this as simple as having a RewriteCond that tests if the request was NOT /jfk/jfk2/(.*) and then doing the RewriteRule?
Or will I have to test for each file like calls to index.php and proc.php?
I do not know the commands available or their syntax and the best approach may not be along these lines, I just do not know.
If the RewriteCond/RewriteRule method is good could someone please show me the structure of the commands to use so I can get this site up and running?
The obvious current drawback is that everytime a teacher (this is a school website I volunteered to help rewrite) creates a new folder they would have to remember to also put a copy of the htaccess file in that folder or it breaks the template.
Thanks.
Then I use two RewriteCond to test if the requested file is NOT /jfk/jfk2/index.php or /jfk/jfk2/proc.php before it performs the RewriteRule. I do this so that I do not end up in a loop.
How could I write a condition that says do not process ANY requests where the requested files is in /jfk/jfk2? I would rather eliminate any processing of files in that folder than having to specify every file I want to exclude but I do not know the syntax. All of my tests have rolled down the processing to all files in all subfolders as well so I am getting something wrong.
My current working script
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/proc.php [NC]
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/proc.php [NC]
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/index.php [NC]
RewriteRule \.(html¦htm¦php¦php4¦php5)$ /jfk/jfk2/proc.php?url=%{REQUEST_URI}&%{QUERY_STRING} [R]
Any suggestions would be appreciated. Or if there is a better way of doing the same thing I would like to know. I just played around with a lot of other example scripts to figure out what they were doing and how to modify them to my own use and assume there are more efficient means than I am aware of.
See RewriteOptions inherit in the Apache mod_rewrite documentation [httpd.apache.org].
> Any suggestions would be appreciated.
You can shorten up your new code and make it a bit more efficient:
RewriteEngine on
RewriteCond $1 !^jfk/jfk2/(proc¦index)\.php$
RewriteRule ([^.]+\.(html?¦php[45]?)$ /jfk/jfk2/proc.php?url=$1 [QSA,R=302]
You may also mark a group as optional. In this example "php[45]?" matches "php4", "php5", or "php".
$1 contains the string matching the parenthesized sub-pattern -- in this case, the requested URI, and QSA means to append the original query string.
Also, a period in patterns mean "any single character," and should be escaped as shwon if you wish to match a literal period, unless it is in a [group]. The group "[^.]+" means, "one or more characters NOT equal to a literal period.
Note that you must change the broken pipe "¦" characters back to solid pipe characters before use; posting on this forum modifies the pipe character.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/jfk/jfk2/(default¦index).(html¦htm) [NC]
RewriteRule \.(html¦htm¦php¦php4¦php5)$ /jfk/jfk2/index.php [R,L]
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/proc.php [NC]
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/index.php [NC]
RewriteRule \.(html¦htm¦php¦php4¦php5)$ /jfk/jfk2/proc.php?url=%{REQUEST_URI}&%{QUERY_STRING} [R]
I will implement your suggestions. I knew it could be condensed but just did not know enough about the syntax to begin myself.
One more question though. I am detecting if a call is made to the default.html or index.html files so it can be redirected to index.php but can I also cause the htaccess file to skip processing requests to ANY other file in the /jfk/jfk2 folder?
In other words, rather than testing for files to exclude like proc.php and index.php can I essentially say end the htaccess script if the requested file is in the jfk2 folder?
This way I do not have to test for every possible file that might reside in the folder and it will only process for files below that folder level.
I have a number of files in the jfk2 folder that should not be processed. Currently they do not appear to cause a problem even without testing because the files are not called directly but included into the page from my index.php. I can forsee other files that would not be used the same way though and would not want to have to modify the htaccess file every time a new file is needed in the root.
Thanks very much for your help especially in the explanation of the rules involved in the code you posted. I have a very difficult time reading books on any general subject and learn mostly by disection of sample code though with this type of code it is hard to determine function without knowing the basic rules and syntax. :)
Thanks again.
Yes, by preceding those other rules with:
RewriteRule ^jfk/jfk2/ - [L]
This ends mod_rewrite processing only; It does not "end the .htaccess script."
You may also use the [S=n] (skip n rules) flag to skip over a certain number of following rules. However, I find this approach difficult to maintain when skipping over more than just a few rules -- If you add a new rule and forget to update the skip count on the preceding rule, bad things can happen.
Jim
I implemented the changes you showed in your first response but ran into trouble. For some reason $1 fails. I did a lot of testing and could not get the script to work with $1 but it works when I substitute %{REQUEST_URI} in it's place.
Here is the currently working version.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/jfk/jfk2/(default¦index)\.(html?) [NC]
RewriteRule ([^.]+\.(html?¦php[45]?))$ /jfk/jfk2/index.php [R,L]
RewriteCond %{REQUEST_URI}!^/jfk/jfk2/(proc¦index)\.php$
RewriteRule ([^.]+\.(html?¦php[45]?))$ /jfk/jfk2/proc.php?url=%{REQUEST_URI} [QSA,R=302]
I had to add another closing paren to the match string in both RewriteRule lines as well. This is working but is the closing paren in the correct location or should it be closing before the section determining the extension?