rack-rewrite - rails3.2 - heroku - redirect hardcoded image urls - ruby-on-rails

My images were all held in the app itself and so were referenced with /images/12345.jpg which in some places has been hard coded into the content of the cms, with or without the full url.
The images have now been moved to s3 and so I want to add a redirect for urls that are of the following formats:
/images/12345.jpg|png|gif
or
http://www.example.com/images/12345.jpg|png|gif
(but only for only numeric filenames)
and point them to
http://my.images.images.s3.amazonaws.com/540x310/12345.jpg|png|gif
I currently have
use Rack::Rewrite do
rewrite %r{images\/(\d*.)(jpe?g|png|gif)$}, 'http://my.images.images.s3.amazonaws.com/540x310/$1$2'
end
But this doesn't seem quite right.
UPDATE------
of course the URL in the source doesn't change, I should have realised that. Clicking the link directly returns an error:
No route matches [GET] "/http://my.images.images.s3.amazonaws.com/540x310/13135.jpeg"
(note the leading slash)
So the rewrite is working but it thinks it should be routing to an internal link not an external URL.
UPDATE2------
Changing to
r302 %r{(?:images\/)(\d*.jpe?g|png|gif)$}, "http://my.images.images.s3.amazonaws.com/540x310/$1"
will now redirect if I go to
http://www.example.com/images/13135.jpeg
but is not rendering the images in the site itself.

this was caused by the url on the localhost still pointing to the live url and therefore the 301 rewrite never being called.
So the local deployment had the live URL hardcoded into the page, i was then changing this and pointing my browser at localhost.
In the page though the old URL remained which meant the page was looking to the live server for a possible redirect which had never been dpeloyed

Related

Styles Missing After Requiring SSL

After requiring SSL in a blank ASP.NET MVC application, some of the content is no longer loading. Specifically, at least one stylesheet isn't taking effect as shown in the screenshot below. Nothing appears in the console. I'm assuming that I need to change an HTTP reference to HTTPS somewhere, but it looks like all the script files are included in the project rather than being referenced online.
Any suggestions on how to fix this?
This is because the CSS file is being referenced over http:// but the webpage is on https://
It’s known as a ‘Mixed Content’ error
If your site works on both http:// and https:// then there are 2 options
Make the http:// version of your site redirect to the https:// version (recommended)
Make the links to your css file ‘scheme relative’ (also known as ‘protocol relative’) by using just // (two forward slashes) instead of http:// or https://
e.g. //css/mycssfile.css instead of http://css/mycssfile.css
The scheme relative urls (starting with //) will use the same protocol as the web page.
So if your page is on https:// then //css/mycssfile.css means use https:// to get the css file. The same goes for http://, // means use http:// to get the css file.

rails application redirecting without the prefix

I have a problem with my web app..the urls all work fine with the exception of the following case.
When I try to access my app without the www prefix
http://myapp.com/mypath
the url changes to www.myapp.commypath - no slash between domain name and the path
every other combination of the url works just fine, but this one, any ideas?

ASP.NET MVC server path is different from application path

I have an unusual circumstance where our web server inserts a folder into the url path before loading the page. Let me give you an example:
The app is called equipment and if I were to run it on a normal server setup, it would look like:
www.site.com\equipment\home\index
BUT when I run it on our server, it inserts "idn" in the url:
www.site.com\idn\equipment\home\index
The messes up my relative references. The MVC functions want to redirect to use "\equipment\" instead of "\idn\equipment\". This happens with Scripts.Render(), Return View(), etc.
Also, I can't hardcode the "idn" into my urls b/c then it is no longer relative and it won't work on my dev box or test servers b/c they don't have a "idn" subfolder in localhost.
I also tried functions such as Request.ApplicationPath and what not but none of them return the "idn" in the result.
Is there way to MVC to know that this "idn" was inserted into the url and account for it?
Thanks!
Create your application on the test/production server in the idn folder, then it all works.

Grails: subfolder is ignored

I have a grails app running on a URL something like -
http://{ip address}/appName
Now I am trying to have a sub-folder of my domain pointing to that app server.
Something like -
http://www.example.com/subfolder/appName => http://{ip address}/appName
When I load the url with subfolder the grails app responds properly and redirects to the first page of the app (as expected). But the problem is - all other URL's in the page (controllers, css, image files, etc.) ignores the "subfolder" at the end of the URL.
Like, instead of trying to point to
http://www.example.com/social/appName/{controllerName}
http://www.example.com/social/appName/{cssFileName}
etc
Everything points to
http://www.example.com/appName/{controllerName}
http://www.example.com/appName/{cssFileName}
etc
I have tried modifying grails.serverURL and grails.app.context. But still the same problem.
You could try:
grails.app.context="/social"
See also: grails 2.0 - correct use of serverURL for production?

Is a URL starting with // valid?

We can see many HTML page using src="//example.com/myjavascript.js" to include a .js and let the browser use http:// / https:// depending on the current scheme of the page URL.
Is this a valid URI ?
Yes, it is definitely valid. It is a "scheme relative" or "protocol relative" URI. In the spec since the beginning. Quite helpful to deal with http/https issues.
Read a much better description and caveats:
Is it valid to replace http:// with // in a <script src="http://...">?
A few points to keep in mind:
There are some minor issues on IE 7 & 8 with double downloads
If you are viewing an HTML page from a file the browser will replace the scheme with file://, and not load your JS file from the server like it would with a full URL starting with http:// or https://.
Edit for modern webdev practices:
While the URL is still valid, it is now recommended to use https for third party resources (and, serve those resources from secure pages). The performance or compatibility issues from years ago are largely resolved with updated protocols and browsers.
As a warning... we encountered issues with this methodology NOT being supported in Internet Explorer when used inside the HTML BASE tag (IE8 and 9 confirmed, didn't test any others before patching the issue).
After newly implementing SSL on a website, we had initially updated the BASE tags across the application to simply reference "//www.mydomain.com" : but the page templates broke (in IE only) on all scripts which did NOT live at the root of the site. Makes sense, since the BASE tag was not picking up the stylesheet or other relatively-linked resources which assumed they were being referenced from root, not from a nested location.
Since the BASE URI was our own domain anyway, we fixed the issue by setting the BASE tag's value to "https://www.mydomain.com". No matter if the client is browsing the site in SSL mode or out, at least it will work cross-browser now and can ALWAYS find its content - and by forcing https at all times, we ensure we don't get any content-mismatch warnings in the process by setting the BASE path to an http location while browsing a page using https.
It is valid indeed and I have just learned this in the hard way!! We broke a customization feature so you should always validate an URL like this (in .NET):
Pay attention to:
White spaces (Trim)
"scheme relative" URI (UriKind.RelativeOrAbsolute)
public bool IsValidUrl(string resourceUrl)
{
Uri uri;
if (Uri.TryCreate(resourceUrl.Trim(), UriKind.RelativeOrAbsolute, out uri))
{
return uri.IsWellFormedOriginalString();
}
return false;
}

Resources