.htaccess - Please assist in mod rewriting application/views directory to "views" in Kohana -


due way have chosen template website, needing rewrite "application/views" "views". have chosen (1) shorten url i'll using in linking stylesheets etc covering structure of file system.

currently, if remove rewrite rule can access media files directly @ application/views/template/file.css. when enable rewrite rule redirected views/template/file.css kohana returns: kohana_http_exception [ 404 ]: unable find route match uri: template/u_crossbrowser/css/bootstrap.css

i imagine dig script , make condition stating if url calling views dir, not try control routing. imagine there better solution.

my .htaccess file:

rewriteengine on  # installation directory rewritebase /  # protect hidden files being viewed <files .*>     order deny,allow     deny </files>   #we needed direct file access our media files (css, js, images) rewrite below breaking it. so, replaced rule 2 below.     # protect application , system files being viewed     # rewriterule ^(?:application|modules|system)\b - [f,l]  # rule 2: disable directory listings indexignore *  # allow files or directories exist displayed directly rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d  # rewrite other urls index.php/url rewriterule .* index.php [pt]   rewritecond %{the_request} ^get\ /application/views/ rewriterule ^application/views/(.*) /views/$1 [l,r=301] 

you have 2 problems here:

  1. your rule rewrite /application/views/... /views/... after rule rewrites everything index.php. need put more-specific rewrite before more-general rewrite prevent more-general 1 stopping processing.

  2. your rewrite rule incomplete. have rule requests /application/views/... rewritten if files @ /views/.... however, since still storing files in application/views/ folder, missing rule lets access them as if @ /views/....

here 1 way make rewrite rules in order work. (i left out rule redirects /application/views/... /views/... because don't see why need - make /application inaccessible , use /views/... , won't matter anyway.)

rewriteengine on  # installation directory rewritebase /  # protect hidden files being viewed <files .*>     order deny,allow     deny </files>  # pretend views 1 directory above rewriterule ^views/(.*) application/views/$1 [l]  # protect application , system files being viewed, # unless redirecting them rule rewritecond %{env:redirect_status} ^$ rewriterule ^(?:application|modules|system)\b - [f,l]   # allow files or directories exist displayed directly rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d  # rewrite other urls index.php/url rewriterule .* index.php [pt] 

(thanks @cduv's answer mod_rewrite: allow redirect prevent direct access env:redirect_status trick.)


in truth, may find makes more sense create route content. because allows move application, modules, , system directories out of web root. 1 benefit of doing can share modules , system between multiple kohana applications if ever develop one. means don't need rely on htaccess protect access these directories.

here simple method can use serve these files, based on kohana userguide module.

add route in bootstrap.php:

// static file serving (css, js, images) - add more extensions if needed route::set('media', 'views(/<file>)', array('file' => '.+\.(css|js|jpg|png)'))     ->defaults(array(         'controller' => 'media',         'action'     => 'media',         'file'       => null,     )); 

add controller controller_media:

class controller_media extends controller {     public function action_media()     {         // file path request         $file = $this->request->param('file');          // find file extension         $ext = pathinfo($file, pathinfo_extension);          // remove extension filename         $file = substr($file, 0, -(strlen($ext) + 1));          if ($file = kohana::find_file('views', $file, $ext))         {             // check if browser sent "if-none-match: <etag>" header, , tell if file hasn't changed             $this->check_cache(sha1($this->request->uri()).filemtime($file));              // send file content response             $this->response->body(file_get_contents($file));              // set proper headers allow caching             $this->response->headers('content-type',  file::mime_by_ext($ext));             $this->response->headers('last-modified', date('r', filemtime($file)));         }         else         {             // return 404 status             $this->response->status(404);         }     } } 

Comments