Redirecting Sub-directories with Backbone.js and/or htaccess - .htaccess

After many hours of research and tinkering, I've finally managed to get backbone.js routes working, with one exception:
If I enter "/workingdir/routepath" - everything is great and it uses the 'routpath' route, however if I enter "/workingdir/routepath/" or "/workingdir/routepath/asdf" or anything similar, my website breaks and I get errors that look somewhat like this:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://example.com/staging/personal/css/style.css". fj:5
Resource interpreted as Script but transferred with MIME type text/html: "http://example.com/staging/personal/scripts/jquery-1.7.2.min.js". fj:6
Uncaught SyntaxError: Unexpected token < jquery-1.7.2.min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://example.com/staging/personal/scripts/nav.js". fj:6
Resource interpreted as Script but transferred with MIME type text/html: "http://example.com/staging/personal/scripts/underscore-min.js". fj:6
Uncaught SyntaxError: Unexpected token < underscore-min.js:1
Resource interpreted as Script but transferred with MIME type text/html: "http://example.com/staging/personal/scripts/backbone-min.js". fj:6
Uncaught SyntaxError: Unexpected token < backbone-min.js:1
Uncaught SyntaxError: Unexpected token <
And the page has no styling whatsoever. I think the issue is coming from the slash in the URL, making the paths for css and js files point to the wrong location (they should be in "staging/css/" and "staging/scripts/", but as the log indicates, it's looking for "staging/personal/xxx").
Any ideas on how to fix this? Thank you for your time!

Relative path references will be resolved differently depending on whether the request URI has a trailing slash or not. I imagine that's what's causing your problem. Consider these links:
<!-- Relative path references -->
<link href="staging/css/style1.css">
<link href="./staging/css/style2.css">
<!-- Absolute path reference -->
<link href="/workingdir/staging/css/style3.css">
This is how those paths will be resolved depending on the request URI:
REQUEST_URI = /workingdir/routepath
/workingdir/staging/css/style1.css
/workingdir/staging/css/style2.css
/workingdir/staging/css/style3.css
REQUEST_URI = /workingdir/routepath/
/workingdir/routepath/staging/css/style1.css
/workingdir/routepath/staging/css/style2.css
/workingdir/staging/css/style3.css
So if you want the ability to make the request with a trailing slash or additional segments, you'll want to use an absolute path reference.

Just to share with others having this issue. I had the same problem, but now with a different solution. These additions to .htaccess didn't work out initially:
AddType application/x-javascript .js
AddType text/css .css
AddType text/javascript .js
AddType text/css .css
AddType font/ttf .ttf
AddType font/eot .eot
AddType font/otf .otf
AddType application/woff .woff
AddType font/woff .woff
AddType font/opentype .woff
AddType application/x-font-woff .woff
AddType application/vnd.ms-fontobject .eot
AddDefaultCharset UTF-8
Options -Indexes
Finally, I realized that while my path below worked on localhost, the CASE-sensitive server did not accept the path
\js\prettyPhoto\js\jquery.prettyPhoto.js
instead of
\js\prettyphoto\js\jquery.prettyPhoto.js
The CAPITAL p was making all those errors! It might be too, for your case, since you have "Uncaught SyntaxError: Unexpected token < " and "Resource interpreted as Stylesheet but transferred with MIME type text/html:"
Let me know if it help : )

Related

Access-Control-Allow-Origin error for Webfonts on Firefox/Mac

i try to load some web fonts from webtype but on Firefox/Mac i always geht an Access-Control-Allow-Origin-Error.
htaccess:
Header set Access-Control-Allow-Origin "*"
base href is also set:
<base href="http://XXX.XXX.ch">
Error: CORS-Header 'Access-Control-Allow-Origin' missing
Any idea to solve the problem?
thanks
thomas

How to add rules in .htaccess via WordPress function

I am trying to add rules in .htaccess via WordPress function which works perfect but always need to update permalink structure to get it added in .htaccess. How can I refresh / reload .htaccess automatically so that new rules can be added without updating permalink structure.
function add_to_htaccess( $rules ) {
$content = <<<EOD
\nAddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
SetOutputFilter DEFLATE\n\n
EOD;
return $content . $rules;
}
add_filter('mod_rewrite_rules', 'add_to_htaccess');
When I say I have to update permalink structure, I mean I have to go to WP Dashboard > Settings > Permalinks and have to click 'Save'
You can use the flush_rules() WP function to refresh the rewrite rule cache programatically (which also updates the .htacccess), without going to Settings -> Permalinks and having to click save.
Please, note that it is not recommended to call this function on each page load, because it can greatly reduce performance. Instead, make sure that it is called only when necessary.
For more information about it, you can refer to this codex link: http://codex.wordpress.org/Rewrite_API/flush_rules
EDIT Use the following code to flush the rules on plugin activation.
// calling this function will make flush_rules to be called at the end of the PHP execution
function myplugin_enable_flush_rules() {
global $wp_rewrite;
// flush the rewrite rules
$wp_rewrite->flush_rules();
}
// on plugin activation, call the function that will make flush_rules to be called at the end of the PHP execution
register_activation_hook( __FILE__, 'myplugin_enable_flush_rules' );

404.17 Error showing after adding a Wildcard '*' Mime type (IIS 7)

I am trying to make extensionless url's (domain.com/urlwithoutextension) to be readable by my web server as an .html or .asp file, and all I'm getting is
HTTP Error 404.17 - Not Found
The requested content appears to be script and will not
be served by the static file handler.
Then below the error it says I can try this:
If you want to serve this content as a static file, add an explicit MIME map.
So I added a MIME map in IIS 7..
Extension: *
MIME Type: application/octet-stream
But I'm still getting this error. All I want to do is make it so domain.com/thisurl to be viewed as a .html file, without the .html extension. Help?
I simply changed it to . instead of * for Extension in MIME Type, and it worked perfectly. Thanks to the comments.

nanoc does not find my PHP files on autocompile

I'm using nanoc to build a static website which has a PHP script for sending mails. Simplified structure:
/content/index.html
/content/contact.html
/content/mail.php
However, when I do a nanoc compile everything is fine in my output folder:
index.html
contact/index.html
mail/index.php
But I can't call the PHP script when doing a nanoc autocompile. /contact/ works, but /mail/ does not.
This is a part of my Rules-file:
route '*' do
if item.binary?
# Write item with identifier /foo/ to /foo.ext
original_filename(item)
else
# Write item with identifier /foo/ to /foo/index.extension
item.identifier + "index.#{item[:extension]}"
end
end
PHP is treated as non-binary. Does anyone know how I can get this to work with autocompile?
If anybody is having similar problems: I found the answer:
The autocompiler does not have support for PHP files. If a directory
is requested, the autocompiler looks for an index.html file in it, but
it will ignore an index.php file. The autocompiler can’t find a MIME
type for it, so it sends the file as application/octet-stream back to
the browser.
It's pretty obvious, but I did not have in mind that autocompile runs a light-weight server with no PHP support (of course).

Kohana modules -path .htaccess protection and media -files

In Kohana there's modules -path protection in .htaccess
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
How could I allow paths like:
http://localhost/modules/mymodule/media/js/myjavascript.js
I would like to include javascript and other media files to my module and still protect other module files like .php
I could allow whole modules -path, but then all .php -files would be listed too.
# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]
Sure there is basic PHP -protection, but I still won't want that anyone could list my modules -path.
<?php defined('SYSPATH') or die('No direct script access.');
The best solution would be to serve those files using a media controller. So a user could request "js/script.js" and Kohana would load the first file it finds using the cascading file structure. There's a good media controller that comes with Kohana, it's in the Userguide module:
Line 247 of classes/controller/userguide.php
public function action_media()
{
// Get the file path from the request
$file = $this->request->param('file');
// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);
// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media/guide', $file, $ext))
{
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
// Send the file content as the response
$this->response->body(file_get_contents($file));
// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file)));
}
else
{
// Return a 404 status
$this->response->status(404);
}
}
This wont be the fastest solution, but if you correctly set the response headers the files should be cached on the client browser.
Solution, add this RewriteCond just before RewriteRule
# Protect application and system files from being viewed
RewriteCond %{REQUEST_URI} !^(.*/)*(application|application/cache|modules/[^/]*)/media/.*$
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

Resources