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