I'm asking about the ability if I can use .js file as .PHP extension for example :
<script src="exampl.php">/script>
and if this method causes any problem ?
the second question is, if I make a url rewriting for making the extension to .js, how can I block the direct accès to example.php, and allow it when the visitor go to example. j's
I'm so sorry for my bad English, please help me and thank's to everyone how will participate :)
Assuming you are using Apache as a web server it would be better to create a rewrite rule:
Create or edit .htaccess and add: RewriteRule ^example.js$ example.php [L]
For the second part you could check $SERVER_['HTTP_REFERER'] or use CSFR token method.
Related
I am trying to copy this website: https://pslk.net/testtest, it redirects to https://pastelink.net/testtest using javascript and not server-side redirect.
I know how to redirect using .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ https://example2.com/$1 [R=301,L]
and Javascript:
<script>
location.href = "https://example2.com" + document.location.pathname;
</script>
But I want to redirect using javascript not htaccess. so My question is: Where should I put my javascript code above for it to be executed on all web pages?
Because I tried to place it in index.html and it did not redirect on all web pages. It will only redirect if I visit the index.html
For example, if I visit this link: example.com/test it should redirect to example2.com/test. but I can't just create multiple folders on my website containing that javascript I am tired , there are multiple numbers of combinations on the website's pathname, and I don't think this website created multiple folders with that javascript redirect: https://pslk.net/testtest2
Sorry I am VERY new on this, Thanks.
I tried this solution, and now it works:
On .htaccess:
RewriteEngine on
ErrorDocument 404 /index.php
On index.php
<script>
location.href = "https://example.com" + document.location.pathname;
</script>
So, on a blank website with only index.php exist. Whatever you type on your website's pathname will go to the 404 page. you can set the Custom 404 page using htaccess and from index.php you can now set the Javascript wildcard redirect. Please tell me if you have best alternatives. Thanks
You can try it with a timeout function:
<html>
<body>
<script>
setTimeout(function(){
window.location.href = 'https://stackoverflow.com';
}, 3000);
</script>
<p>Redirect after 3 seconds.</p>
</body>
</html>
sm3sher is right. You'll have to put the script-tag and its content within the body tags and it should be the last item there.
Based on what you describe having an index.html, it seems like you are not using any server side rendering like php. I also don't think you don't have any templating engine (server side or client side)..
If all your pages are somefile.html then you have to edit all your files manually.
I have been working on the project where user should be able to click on the link and open excel file. My files are sitting on the server folder, I use JavaScript onClick function to open excel files. This way of opening files is very insecure because user can see direct link to the folder(if they open devTools). I would like to use ColdFusion function that will grab files from the folder and server them to the user. I was wondering what is the best way to do that in ColdFusion? Is there and functions that directly look in the folder and servers the file? Here is my current code:
<cfdirectory directory="#exportsDir#" action="list" filter="*.xls" recurse="no" name="fileList">
<cfoutput query="fileList">
<cfset href = "../Reports/#fileList.name#">
<div onClick="location.href = '#href#';">#fileList.name#</div>
</cfoutput>
If anyone have suggestions on how to approach this problem please let me know. Thank you!
To avoid outputting the path to the file there are several ways to do so.
URL rewrite
One is to use a URL rewriter. In Apache, for example, you can enable it via the mod_rewrite module. This looks then something like
RewriteEngine On
RewriteBase "/files/"
RewriteRule "^(.+)$" "../Reports/$1" [L]
ColdFusion script
Another way is to let a ColdFusion script serve the file, so the path to it is hidden behind the script. See How can I force a download of a pdf in a url?
If i put <base href="http://www.example.com"/> then if I go in my browser to WWW.example.com everything is fine, but when I go to example.com some things with Cascade style sheet files and java script files are screwed (for example icons in bootstrap don't show) until i click some other page on my site so it transforms to WWW.example.com/page and from there everything is fine... I have tried with <base href="http://example.com"/> and going to WWW.example.com and it is same just other way around. How can i fix this?
You can set the base tag to the respective sub-domain that the user has entered. Or you can just redirect all your requests to the www sub-domain with a simple mod_rewrite rule.
Going the first way, I believe you can do that with javascript, not 100% sure though. Try having this at the start of your code, having the base tag with www as default.
if(document.URL.indexOf('://www.')===-1){
var base = document.getElementsByTagName('base')[0];
base.href = base.href.replace('://www.','://');
}
The other way is to use mod_rewrite, which is easily done with .htaccess files in apache.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I personally think the second way would be the better choice. Firstly because I'm not completely sure that the javascript scenario will work at all, and even if it does, javascript is not ran on all browsers and especially on bots. So I'd advice you to use .htaccess. Good luck!
How about edit your DNS information to set that "example.com" link to the real ip and didn`t as a 301 redirect
How can I use only JavaScript and/or .htaccess to map a request for:
http://example.com/map/new_york
to:
http://example.com/map?city=new_york
Without the user seeing the URL redirect?
UPDATE:
Since there seems to be a lot of confusion here, I'll provide more detail. I want to accomplish Search Engine Optimization (SEO) for my map application. The map resides on my web server at http://example.com/map. I want to allow people to deep link to a city of their choosing, which is in the form of http://example.com/map/?city=new_york. Since I want SEO, the URL should instead look like: http://example.com/map/new_york.
Without using any type of server-side programming (PHP/Python/etc), how can I accomplish this use case with only JavaScript and/or .htaccess?
You cannot use JavaScript to do this.
However, you CAN do this via .htaccess, if you have mod_rewrite installed on the web server that is hosting your website.
See this link for creating "Rewrite" entries in your .htaccess file:
http://corz.org/serv/tricks/htaccess2.php
(there are plenty of resources on the internet to do this, search for ".htaccess rewrite"
UPDATED:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^map/([^/]+) /map?city=$1 [NC]
That should rewrite all requests to map/[ANYTHING] to /map?city=[ANYTHING]
var url = 'http://example.com/map/new_york';
var u = url.split("/");
document.write(u[0]+'/'+u[1]+'/'+u[2]+'/'+u[3]+'?city='+u[4]);
http://jsfiddle.net/NzXmd/
Not sure if it's possible but how do I read a resource from a url using javascript without ajax?
for example, the following url is a static text file containing json encoded text
http://mysite.s3.amazonaws.com/jsonencodedcontent.txt
I'd like to use javascript to read the content from above link, read the json content into a javascript variable.
I can't use ajax because of cross site and I have no control over amazon S3 domain.
anyway to achieve this?
Try #Ben's suggestion first. If for any reason that doesn't work in your case, here's two options I've both seen and used, which may or may not be available in your case (I'm providing two overly simplified examples just to clarify my points):
Create a server side resource that resides in your domain and retrieves and returns the cross site content for you:
<?php
die(file_get_contents('http://mysite.s3.amazonaws.com/jsonencodedcontent.txt'));
Use mod_rewrite (or something similar) to create a virtual resource in your domain that resolves to the remote content behind the scenes:
RewriteEngine On
RewriteRule ^jsonencodedcontext\.txt$ http://mysite.s3.amazonaws.com/jsonencodedcontent.txt [P]