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
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:
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).
.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 :)
So I've been pounding my head against the wall for days on this one .. Using Javascript I'm trying to encode the path for API calls to Dropbox. Dropbox takes the following url
https://api.dropbox.com/1/shares/dropbox/<path>
So let's say I have a file 'Getting Started.pdf' in my root folder .. access to the shares or any other api call should be something like
https://api.dropbox.com/1/shares/dropbox/Getting%20Started.pdf
However, I'm returned this error
{"statusCode":404,"data":"{\"error\": \"Path '/Getting%20Started.pdf' not found\"}"}
Removing the url encoding for spaces seems to work, but causes issues for file names with other special character like &
https://api.dropbox.com/1/shares/dropbox/some %26 some.txt
Anyone know exactly how the url is expected to be encoded?
[edit] Here is my encoding function
p = encodeURIComponent(utf8)
.replace(/%2F/g, '/')
.replace(/\)/g, '%29')
.replace(/\(/g, '%28');
Ok so I've answered my own question after a sleepless night .. here's how to correctly encode in JavaScript (also for node.js)
p = encodeURIComponent(p)
.replace(/%2F/g, '/')
.replace(/\)/g, '%29')
.replace(/\(/g, '%28')
.replace(/!/g,'%21');
if (p[0] === '/') { p = p.slice(1); }
Also we were adding the path to the body of the request as well as the url .. big mistake. Make sure that the body of the request ONLY contains expected parameters.
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.