RewriteRule for Javascript/css versioning - mod-rewrite

I wrote a utility to handle the versioning of my CSS and JS files for caching purposes, however, I'm struggling to get the RewriteRule setup correctly to load the original file.
The way the versioning utility writes the new URL is as below:
Local
<script src="20110125/contact.js" type="text/javascript"></script>
Global
<script src="../Scripts/js/20110125/core.js" type="text/javascript"></script>
My RewriteRules strip out the timestamp and load just the path and filename. They are as follows:
#rewrite core js
RewriteRule ^(\/Scripts\/[a-z]*\/)[0-9]*\/(.*) $1$2 [NC]
#rewrite directory level js
RewriteRule .+\/(.+\.js) $1 [NC]
However, when I make a page request I get a 404 on the pages. Any help is appreciated.

You have a rather bizarre path to your JS files; I'd recommend just using /js or /javascript, and don't use capital letters in file or path names in a URL. What are you trying to do with your second RewriteRule?
# /js/123456/jquery.js to /js/jquery.js
RewriteRule ^/js/[0-9]+/(.+).js$ /js/$1.js [L]

Related

Can't get an image asset

Why i can't get an image to work. I'm trying this:
background-image: url(jBootstrap/images/ui-icons_222222_256x240.png);
and it doesn't work. When i'm trying to access it trought URL i get an error:
Asset [stylesheets/jBootstrap/images/ui-icons_222222_256x240.png] was unable to be processed.
Can't get any images, tried in many variations of directories and still doesn't work. When i'm doing this on plain html-css it works perfectly, but not in laravel. What am I doing wrong ?
btw, i'm using Basset if it helps.
EDIT 1
Also including the additional information about a partial structure of my public folder and html link generated by basset:
public/
stylesheets/
jBootstrap/
images/
ui-icons_222222_256x240.png
main.css it contains the background-image...
when including the css file, in source code i see:
<link rel="stylesheet" type="text/css" href="http://localhost/Job/worker/myapp/public/7n3C5wypAmTi8VT8/application/stylesheets/main.css" />
Edit 2
Adding my public/.htaccess file:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Edit 3
Tried another way:
public/stylesheets/main.css
public/stylesheets/abc.jpg
in main.css I have code: body { background-image: url('abc.jpg') } and i'm getting nothing ...
Please help, struggling 2 days now ...
Found the problem... It was Basset package, it wat required to put $collection->apply('UriRewriteFilter'); into configurations.
I would naturally assume that one (or both) things are incorrect here:
Your .htaccess file is not checking for existing files when rewriting. Check to see if RewriteCond {%REQUEST_FILENAME} !-f exists in that file.
You are not requesting your background image from the root, i.e. absolutely. Try requesting the resource using a leading slash. For example, instead of calling foo.png, try calling /foo.png.
Try using setArguments('../') on your collection
'collections' => array(
'application' => function($collection) {
$collection->add('../vendor/twitter/bootstrap/less/bootstrap.less')->apply('Less');
$directory = $collection->directory('assets/stylesheets', function($collection) {
$collection->add('main.css');
})->apply('UriRewriteFilter')->setArguments('../')->apply('CssMin');
}
),
This did the trick for me.

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

Problem with mod_rewrite and relative paths of included css, js, image,... files and links

First of all - I have searched quite a bit for an answer in stackoverflow and via google but haven´t been successfull so far to find a possible solution any advice would be greatly appreciated.
problem is:
I have a page
www.mypage.com
and controll the language displayed via a GET parameter lang
-> www.mypage.com/index.php?target=1&lang=en
Now a client has registered a domain at united-domains (www.mypage.it) and wants me display the italian version of the page whenever the domain www.mypage.it is requested.
The provider united-domains offers a solution called URL-HIDING which basically seems to pass the the request to a URL+folder structure I provide (like www.mypage.com/lang/it - according to the specifications of united-domains it has to be a folder structure and may not be a file)
solution so far:
Calls to the domain: www.mypage.it will be maped to www.mypage.com/lang/it/ via the URL-HIDING option.
there an .htaccess file rewrites the REQUESTS to the actual target:
RewriteEngine on
## check if query string contains NOT 'lang='
## (lang might be changed by user after initial request)
RewriteCond %{QUERY_STRING} !lang=
## redirect to page
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## check if query string contains 'lang='
RewriteCond %{QUERY_STRING} lang=
## Keep the existing query string using the Query String Append flag
RewriteRule ^(.*)$ http://www.mypage.com/index.php? [QSA]
Problem is of course: the CSS, javascript, image,... files are included with an relative path
<link href="scripts/style.css" rel="stylesheet" type="text/css">
<script src="scripts/media.js" type="text/javascript">
<img src="images/logo.jpg">
PDF
therfore the relative request scripts/style.css is mapped to http://www.mypage.com/lang/it/scripts/style.css
I am sadly stuck the the URL-HIDING mechanism of united-domains and changing every relative path the /scripts/media.js is not an option as I´d have to change quite a bit of code.
Does anyone have a solution for this (my .htaccess knowledge is not the best I am afraid)
Couldn´t I just remove lang/it/ from EVERY request and additionally check if the QUERY_STRING contains the string lang= and if it does simple add ?lang=it
thanks to all that have taken the time to read so far - if anyone has a suggestion I´d be more than grateful !
stay well,
matthias
This seems to do the trick !
in folder:
www.mypage.com/lang/it/
I put:
RewriteEngine on
## redirect if just folder is requested
RewriteCond %{REQUEST_URI} ^/lang/it/$
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## otherwise redirects if file does not exist
RewriteCond %{DOCUMENT_ROOT}/lang/it/$1 !-f
RewriteRule ^(.*)$ http://www.mypage.com/$1 [QSA]

jQuery - Address plugin problem

I've come across a problem which i cant seem to figure out, i use the jQuery address plugin to store history and enable deep linking, and a typical url after a click would look like this:
http://mysite.com/#!/page
Problem here is i need rid of the last / so i need it to look like this:
http://mysite.com/#!page
I'm using plugin version 1.2 - the latest is 1.4. When i use 1.4 my hashbang #! disappears..
Anyone know why? even so, the updated version produces the same problem.
Reasons to fix this are i use 301 redirects to 'Pretty URL's' if an ?_escaped_fragment_= is requested. So this:
http://mysite.com/data/#!page1
would become:
http://mysite.com/data/page1
currently it does this: mysite.com/data//page1
here is the .htaccess rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite current-style URLs of the form 'index.php?url=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>
and here is some relevant PHP i use on page load:
if ($fragment = $_GET['_escaped_fragment_']) {
// OPTION 1: if Google is reqesting an '_escaped_fragment_=' page, then redirect to a clean URL
header("Location: $base/$fragment", 1, 301);
exit;
}
Any help on how to make this situation better is appreciated.. I don't want 'use the HTML5 History API' as ive explored this option already.
This line of code can help!
$.address.strict(false);

CodeIgniter CSS File Not Included

I have a bit of an odd problem with my CodeIgniter installation, I am using modrewrite in order to shorten my URLs, for example I have an api page:
http://www.mydomain.com/api
And it works really well, my .htaccess file looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But whats really strange is if I type a trailing slash:
http://www.mydomain.com/api/
The API page loads ok but it doesn't include the Base CSS file which is included on all the pages, it would be fine if it didn't load anything at all, but I need to stop it from loading the actual api page. Any advice would be helpful,
Thanks!
UPDATE: I have found that when I view source my CSS file comes from the following when the CSS loads correctly:
http://www.mydomain.com/css/all.css
But when it fails to load it comes from
http://www.mydomain.com/api/css/all.css
Make sure that you have a trailing slash in your base_url in the config.php file:
$config['base_url'] = "http://www.mydomain.com/";
And always call the base_url() when dealing with links...etc
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/all.css">
EDIT:
Please note that I'm using the mod_rewrite from this wiki page BUT since my CI installation in not on the root (inside /ci173/) the RewriteBase is not / but /ci173/.
When using relative URL path like css/all.css or ./css/all.css (both are equivalent), you need to be aware that relative URL paths (like any relative URLs) are resolved using a base URL that is the URL of the current document if not specified otherwise.
So in your case /api or /api/ is the base URL path and the relative URL path css/all.css is resolved differently depending on the base URL path:
/api + css/all.css → /css/all.css
/api/ + css/all.css → /api/css/all.css
To conquer this you either need to
use absolute URL paths like /css/all.css that are independent from the base URL path
specify a different base URL in your HTML document (e.g. /); but note that this will affect all relative URLs and not just relative URL paths
adjust your relative URL paths to suite your base URL paths:
for /api use css/all.css
for /api/ use ../css/all.css
for /api/foo/bar/ use ../../../css/all.css, etc.
I guess the first solution is the easiest.

Resources