http redirect to specific https link - javascript

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}

Related

non-www to www client side redirection with javascript

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';

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 :)

How to redirect url using javascript query

How do I redirect conditionally with query /xxx?xxx in the url link?
For example, anyone who come to my site with the below two link should be redirected to different pages.
mysite.com/contest?a => mysite.com/contest-a
mysite.com/contest?b => mysite.com/contest-b
Can this be done using both js and meta refresh?
How do I do this using .htaccess?
If there is a querystring, replace the first instance of ? with -
if (window.location.search.length)
window.location.href = window.location.href.replace('?','-');
Use
location.href
to detect url, where you are now. And
window.location.replace('mysite.com/contest-a ');
to do redirect.
But probably it is better to do it on server side, since it will be faster.
you can use the below script to do the re-direction.
window.onload = function()
{
var location = window.location;
if(location.indexOf("contest?a") > -1)
{
window.location.href = "mysite.com/contest-a";
}
else
{
window.location.href = "mysite.com/contest-b";
}
}
It's not possible to change the url without refreshing the page (except for hashes), and thus you would be better off not using that url in the first place to avoid unnecessary requests and hassle for the user. You could take a look at mod_rewrite which does that (transform a "nice url" from the user into an "ugly url" that your server actually understands)
In your example, a possible rule might be
RewriteRule ^mysite.com/contest-{.+} /mysite.com/contest?$1
If you're ok doing this kind of operation on the client side, you could embed the following snippet on your landing page (the page where the arriving user will be redirected), just after the <head> tag:
<script type="text/javascript">
var mapReferrer = function() {
var gotoURL;
switch(document.referrer) {
case "https://www.google.com": gotoURL = "http://<yoursite>/arrivedFromSearchEngine"; break;
case "https://www.facebook.es": gotoURL = "https://<yoursite>/engageFBusers"; break;
case "http://<yoursite>/contest?b": gotoURL = "http://<yoursite>/contest-b"; break;
};
if (gotoURL) window.location = gotoURL;
};
window.onload = mapReferrer;
</script>

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.

Force open links in same window on same domain

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

Categories