How rewrite all JS, CSS, IMG to static subdomain? - javascript

I have:
sub.domain.com/js/test.js
sub.domain.com/image1.jpg
sub.domain.com/test.css
How rewrite all the file types to:
static-sub.domain.com/....
?
Thanks a lot.

Something akin to what's below should work.
RewriteEngine on
RewriteRule ^sub\.domain\.com/(js/*\.js)$ static-sub.domain.com/$1 [NC,R,L]
RewriteRule ^sub\.domain\.com/(*\.jpg)$ static-sub.domain.com/$1 [NC,R,L]
RewriteRule ^sub\.domain\.com/(*\.css)$ static-sub.domain.com/$1 [NC,R,L]
You'll likely need to tweak it to your specific scenario. Notice the usage of capturing groups ((js/*\.js)) to store the variable path information to be used in the destination ($1). Also note the NC, R and L options; which stand for "no casing" (case insensetive), "redirect" (redirect the request) and "last rule" (stop processing further rules if this rule is a match).

Related

How to overwrite url from window.location.href

I want to show an image and its metadatas by using this kind of url in a browser:
domain/image/imageName
but the actual script to get the image link based on its name is called in:
domain/image.html
I've made this RewriteRule in my htAccess file :
RewriteRule ^image\/(\w+)$ image.html?$1 [NC]
To transforme my "fake" url like this :
domain/image/imageName => domain/image.html?$1
In order to get the image name, I used the window.location.href to see what I've got and my console show me this :
Url = http://localhost/image/ImageName
//and not//
Url = http://localhost/image.html?ImageName
//as I expected
Do someone know how to get what htaccess actually transform or suggest a better method ?
Thank you for your help.
Question answered in comment. I also found a regex way to extract what I got from the js function :
window.location.href.match("[a-zA-Z0-9_\-]+[^\/]$")

Ajax call to Php script to fetch image in base64 string working on localhost but not from hosting server

Description: I have a HTML page which onloading triggers Ajax call. In this Ajax call I'm passing in the ids(which is the same as image name which are placed on server) .Each id sent via ajax function to the php script which then fetches the image convert it to base64 and returns it back to ajax call. On success this Javascript function writes base64 string to(href="base64") the corresponding id it came from.
Problem: Now all of this is working fine on localhost with directives in .htaccess file but just when I placed it on my hosting server the HTML page is making the ajax call to PHP script but the PHP script is not returning the base64 string as on localhost but is returning the markups of Index.html. In my .htaccess file on server I have condition that "# Redirect all requests to index.html" (but that's only to avoid any unwanted requests from user).
Checks performed: 1) case sensitivity of names.
2)Have placed the files in correct directory locations.
3)Compared the requests(using Developers tool,Network tab) that's made on localhost with the one on hosting server and both are same with 'Status 200' (their content 'base64string' and 'markups of index.html' respectively).
HTML
JavaScript
<script type="text/javascript">
$('.example-image-link').each(function() {
var id = $(this).attr("id");
var data = id;
$.ajax({
type: "POST",
url: 'http://mysite.in/home/myname/public_html/image_extract.php',
async: true,
data: {post: data},
success: function(data) {
var x = "data:image/jpeg;base64,";
var y = data;
z = x + y;
document.getElementById(id).href= z;
return false;
}
});
});
</script>
image_extract.php
$q = $_POST['post'];
$main = explode("_", $q);
if($main[0] == "travel")
{
$dir = "images/travel_pics/".$q.".jpg";
$image = file_get_contents($dir);
$imdata = base64_encode($image);
if ($imdata !== false) {
echo $imdata;
}
else {
echo 'An error occurred.';
}
}
.HTACCESS
# Allows ModRewrite to work
Options FollowSymLinks
# Turn on rewrite engine
RewriteEngine On
RewriteBase /
# Redirect all requests to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html
IndexIgnore *
RewriteCond %{HTTP_REFERER} !^http://mysite.in/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://mysite.in$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mysite.in/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mysite.in$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]
Why am I not receiving base64 string from php script on hosting server(Godd*dy)? Thanks in advance. :)
.htaccess looks fine, JS also - it seems like the php file image_extract does not exist.
You are using path http://mysite.in/home/myname/public_html/image_extract.php
try http://mysite.in/image_extract.php instead because /home/myname/public_html is your document root (i suppose).

Url in the Address bar with php, or javascript

.htaccess
RewriteRule ^info/{0,1}$ index.php?page=info [QSA,L]
RewriteRule ^login/{0,1}$ index.php?page=login [QSA,L]
the same : index = index/
RewriteRule ^info/?$ $1
RewriteRule ^login/?$ $1
javascript:
reg_e=/#$/; if(!window.location.href.match(reg_e)) {
window.location.href = decodeURIComponent(window.location.href)+"#";}
it's working... good
I have problem that
when someone try to add '/' or any at the final url http://example.com/info/# will return http://example.com/info# or http://example.com/info/# but page will change nothing ?
Can you help me establish any rules for none breaking web addresses at ends of lines?
example : http://example.com/info# <- base url
http://example.com/info/# to be => http://example.com/info#
or : http://example.com/info/# <= or if anyone add '/#-' e.tc. the web will be nothing change.
And when I click 'info' and add '/' [http://example.com/info/#, it's ok. but i click other link it will be :
http://example.com/info/login# <- RewriteRule ^login/?$ $1
instead of: [http://example.com/login/#
if remove #RewriteRule ^login/?$ $1 ->> http://example.com/login# when add '/' handle => css wont load.
'info' and 'login are the same level in index folder.
i'd tried "<base href='/' /> but not working.
how to have a safe url? thanks your opions.
I am not 100% sure what you want here but it seems as though you want a link to redirect from base URL and not the relative URL.
Say your link is this:
Login
Try this instead
Login
The / at the beginning will say to go back to the base URL and start from there. Let me know if I missed the point completely though :)

encodeURIComponent is failing

So I am trying to create a search in my website and I need to encode some text so it is URL friendly. However, if I search anything with a "<" symbol I get HTTP error 403 (access forbidden) because the "<" is not being encoded.
This is the code I am using:
var search = $("#txtHomeSearch").val();
if(search != ""){
var urlSearch = encodeURIComponent(search);
window.location.href = "/search&s=" + urlSearch;
}
Example of a working url: http://website.com/search&s=helloword
Example of a broken url : http://website.com/search&s=<
Maybe the problem is with my .htaccess file which contains:
RewriteEngine on
RewriteRule ^([^.*]+)$ index.php?page=$1 [L]
ErrorDocument 404 /errorPages/404.php
There is a simple utility here: http://www.the-art-of-web.com/javascript/escape for verifying the operation of the various Javascript escaping functions. Accoring to the ECMA standard, and verified using that tool, the "<" should be escaped correctly by the encodeURIComponent() function.
Could it be a character other than "<" causing the problem? There are various remedies for the characters that encodeURIComponent misses. One is the url_encode function listed here and elsewhere: javascript window.location do I need to escape?
Try escaping your back reference using [B] flag.
RewriteEngine on
RewriteRule ^([^.*]+)$ index.php?page=$1 [B,L]
ErrorDocument 404 /errorPages/404.php

Replace subdomain name with other subdomain Using JavaScript?

I'm trying to replace the subdomain name from "news.domain.com/path/.." to "mobile.domain.com/path/..", using JavaScript
Any idea how to achieve this?
I'm assuming that you want to change a string in the generic format xxxx.domain.com/... into mobile.domain.com/.... This regexp should do it in JavaScript:
var oldPath = "news.domain.com/path/";
var newPath = oldPath.replace(/^[^.]*/, 'mobile')
This should work in normal cases:
"http://news.domain.com/path/..".replace(/(:\/\/\w+\.)/, "://mobile.")
Use following to add an extra level of validation:
function replaceSubdomain(url, toSubdomain) {
const replace = "://" + toSubdomain + ".";
// Prepend http://
if (!/^\w*:\/\//.test(url)) {
url = "http://" + url;
}
// Check if we got a subdomain in url
if (url.match(/\.\w*\b/g).length > 1) {
return url.replace(/(:\/\/\w+\.)/, replace)
}
return url.replace(/:\/\/(\w*\.)/, `${replace}$1`)
}
console.log(replaceSubdomain("example.com", "mobile"));
console.log(replaceSubdomain("http://example.com:4000", "mobile"));
console.log(replaceSubdomain("www.example.com:4000", "mobile"));
console.log(replaceSubdomain("https://www.example.com", "mobile"));
console.log(replaceSubdomain("sub.example.com", "mobile"));
If you want to send user to new url via JS - use document.location = "mobile.domain.com/path/..".
In reference to FixMaker's comment on his answer:
window.location.href will give you a fully qualified URL (e.g. http://news.domain.com/path). You'll need to take into account the http:// prefix when running the above code
A suitable regular expression to handle the request scheme (http/https) is as follows:
function replaceSubdomain(url, subdomain){
return url.replace(/^(https?:\/\/)(www\.)?([^.])*/, `$1$2${subdomain}`);
}
let url1 = 'https://sub-bar.main.com';
let url2 = 'https://www.sub-bar.main.com';
console.log(replaceSubdomain(url1, 'foobar'));
console.log(replaceSubdomain(url2, 'foobar'));
You cannot replace a subdomain. You can redirect using javascript.
<script type="text/javascript">
<!--
window.location = "http://mobile.domain.com/path/to/file.html"
//-->
</script>
I tried using java script but no luck and for my case i use the below code in .httaccess file
RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|android" [NC]
RewriteCond %{HTTP_HOST} !^mobile.domain.com
RewriteRule ^(.*)$ http://mobile.domain.com/ [L,R=302]
it will replace "news" sub domain to "mobile" sub domain. hope it will help any one.

Categories