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.
Related
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_\-]+[^\/]$")
I have a webpage that I would like to redirect to a specific link if the user is coming from http://
<script language="JavaScript">
var loc = window.location.href+'';
if (loc.indexOf('http://')==0){
window.location.href = loc.replace('http://','https://secure.example.com/app');
}
</script>
If the user comes from http://example.com/app or any http:// I would like to redirect it to that exact link.
When I run this JavaScript it is taking https://secure.example.com/app and adding domain.com/app like below
https://secure.example.com/appexample.com/app
Any help with this would be greatly appreciated.
I also tried the meta tag thing
<meta http-equiv="refresh" content="2;url=https://secure.example.com/app" />
But its just keeps refreshing and doesnt feel right with the hesitation page change.
<script language="JavaScript">
var loc = window.location.href+'';
if (loc.indexOf('http://www.')==0){
window.location.href = loc.replace('http://www.','https://secure.');
}
else if (loc.indexOf('http://')==0) {
window.location.href = loc.replace('http://','https://secure.');
}
</script>
It was doing what you describe because you were replacing http:// with https://secure.example.com/app so of course everything that was after http:// will still be there afterwards.
I would recommend simply replacing http:// with https://secure. when there's no www.
To additionally cover cases where there is a www, you can simply replace www. with nothing:
//var loc = window.location.href;
var loc = 'http://www.example.com/app';
console.log(loc);
loc = loc.replace('www.', '');
loc = loc.replace('http://', 'https://secure.');
console.log(loc);
Add this to your server configuration file instead of doing it in html
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
301 REDIRECT should be the better way to do it
for an javascript approach(es6). not the recommended way because browser redirect is not always reliable
return location.protocol != 'https:' ? location.protocol = "https:"
: {do nothing logic}
How can I redirect the following url in javascript :
http://example.com/?querystrings
=>
http://www.example.com
I want to redirect my url without www to www discarding the querystrings.
I know how to do it server side using mod_rewrite, I am working on a client side js app, where I need this.
Is this type of redirection possible with JS?
My code so far :
<body onload="addwww()">
<script>
function addwww() {
location.href="http://www.example.com";
}
</script>
it redirects the entire page to www with redirect loop error. how can I redirect only when there is no www in the url?
Thanks!
This script replace non-www to www:
if(!/\.?www./g.test(location.host)) {
location.href = location.href.replace("://","://www.")
}
As mentioned in the comment, you may try something like
if (location.href.indexOf('www') < 0)
location.href = 'http://example.com';
or
if (!location.href.indexOf('http://www'))
location.href = 'http://example.com';
as a start.
Use regexp to identify www:
if (!(/www/.test(location.href)))
location.href = 'http://www.example.com';
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
could someone help me with this problem.
There is requirement to open all links when they are on external domains in _blank. But links in same domain must be opened in same window. I having issue, because I working in 2 domains one is https://www and other just http:// without www, how to open link in same windows on link without www?
function externalLinks() {
var h = window.location.host;
jQuery("a[href^='http']").not("[href*='" + h + "']").not(".forceSameWindow").attr('target', '_blank');
}
now all links exept https://www.something.com opening in blank example: http://something.com
I must do this in jquery/js. I done this by doing hardcoded domain, but what do do nicely!
Thanks for your help!
Just change
var h = window.location.host;
to
var h = window.location.host.replace(/^www/,'');
that way it doesn't matter if you are on the www host or not
You have to force apache to add www.
Add this to your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]
var externalLinks = function(){
var anchors = document.getElementsByTagName('a');
var length = anchors.length;
for(var i=0; i<length;i++){
var href = anchor[i].href;
if(href.indexOf('http://h4kr.com/') || href.indexOf('http://www.h4kr.com/')){
return;
}
else{
anchor[i].target='_blank';
}
}
};
This should work :)
This is based off your original post
$("a").each(function($a){
$a=$(this);
if(!~this.href.indexOf(document.location.host)||!$a.hasClass('forceSameWindow')){
$a.attr('target','_blank');
}
})
Assuming all links are not set up to _blank initially