Redirect URL using? - html

Is there any way to redirect a URL by having 1 centeral redirect page like:
example.com/redirect
and having a sort of attribute at the end to redirect to a certain page,
example.com/redirect?pagename
and in the code it will tell the webpage to redirect to the desired page.
Sorry if I baffled or anything I'm quite new to this sorta stuff.

That is possible if you can have a CGI script, FastCGI program, or PHP script behind that URL. It can then look at the parameter and dynamically create a reply page with HTML redirect in the header.

You can by adding in your redirect page or
javascript:
<script>
window.location = "http://google.com";
</script>
or
PHP:
<?php
header("Location:http://google.com")
?>

Related

How would I make a meta redirect that redirects to the URL stored in the parameter?

I am attempting to setup a service which will redirect the user to a URL stored in the ?url= parameter, how would I do this?
I have tried using meta
http-equiv="refresh"
with
out.print(request.getParameter("ur;")) %>
however all that happens is the page reloads in a loop. Not too sure if I need to use quotation marks (e.g /?url="https://google.com")
<meta http-equiv="refresh"
content="0; URL="<%
out.print(request.getParameter("redirect")) %>">
What I need to happen is when the user gets redirected to "https://example.com/exampledir/index.html?url=https://anotherwebsite.com" (just an example url) the script on index.html will get the parameter and redirect to it exactly. What actually happens is the page just reloads in a loop.
Since you're not using any server-side language, you won't be able to generate a different meta tag during rendering. Instead, you'll need to do this with JavaScript after the page loads.
Add a script tag to the bottom of the page like this:
<script>
var redirect = new URL(window.location).searchParams.get('redirect');
if (redirect) window.location = redirect;
</script>
Now if you browse to the page like this: http://<myserver>/myfile.html?redirect=https://google.com you'll be redirected to https://google.com.
For reference, see MDN articles on:
Window.location
URL.searchParams
I also highly recommend you read through Java​Script basics just to make sure you understand the overall concepts presented here.

How to block direct access to html page

I have a sub html (template) products.html, that I included in my index.html page with JavaScript/jquery function .load()
Now the problem is products.html can be access directly if you type its url, and it gives a very ugly page alone, so I tried to redirect anyone who tries to access it to index.html using JS function windows.location. The problem with this method is, when my template is loaded in my main page, the js script fires, and it leads to refresh to the page. So is there another way to go about this !?
You can define a variable like window.parentPage = true;
in the index.html file.
In the products.html page make a check like so:
if(!window.parentPage)
{
window.location.href = "YOUR REDIRECTION PAGE"
}
Another alternative:
<body onload="if (document.referrer == '') self.location='index.html';">

Load new HTML page in Parent from iFrame Child

I have an HTML page with an <iframe> that loads an HTML form. The form inside the iFrame posts to a PHP script.
Upon completion of the PHP script, how can it load an entirely different HTML page outside of the iFrame?
not in php, php syntax is
header( 'Location: url' ) ;
but it only works if you havent done anything else yet.
but you can do it with javascript like so
document.location.replace("url")
or
document.location.href = "url";
so with php you can write (after form is submitted)
echo "<script>document.location.replace('url');</script>";
or
echo "<script>document.location.href = 'url';</script>"
edit
i changed window (redirects iframe) to document (redirects page) i should work now, sorry about that.
you can also use top.window.location i think.
a simpler way to do this is in the actual html <form action="url" target="_top">. however this will skip form-validation.

How to handle refresh while using iFrame in page structure

This is my web structure. In which all link contents are opened in iFrame when link is clicked.
Say I have clicked on 1st link (not Home page) , it gets open in iFrame,contents are displayed properly,all ok. But as I press refresh button expected is shlould load same page whose link I have clicked,but instead of that it loads Home page.
How should I be on same page after refresh also?
I'm using php for server side scripting. Thanks!
Edit:
I tried to store visited page name in session variable and then while loading Home page I check like
$_Session['visited']='/pages/neworder.html';
if(isset($_Session['visited']))
{
echo "<script>window.location.href=</script>".$_Session['visited'];
}
But didnt work!
I think the simplest way is with Jquery. If you make your website use anchors, for example
http://yoursite.com/#page-example
example
When you refresh the page, you will still be accessing this anchor. With jquery you can get this value and do some stuff based on that. So!
var hash = $(this).attr('href').split('#')[1];
This will get that # value, you can compare that in javascript then, or use a switch to determine what to do or simulate the page change.
Server side: Link sends GET request to the page and the src of the iFrame is set by the server.
-OR-
Client side: Add onclick function on the links which changes the src atribute for example with the following code:
document.getElementById('Iframe').src="new link";
-OR-
<iframe src="startin_link" name="iframe_a"></iframe>
<p>link</p>
Pure html.
EDIT: If you want to stay in the page... I suggest you use the first option. Example:
<a href='?page=1'>Some link</a>
then on server side:
if(!is_null($_GET['page'])){
$iframeLink=arrayOfLinks[$_GET['page']];
}else{
$iframeLink="Starting Link";
}
and the iFrame should look like this:
<iframe src='<?php echo $iframeLink; ?>'></iframe>

How do I get link to show HTML source?

I'd like my web page to have a "Show Source" link that will show the source of my HTML.
I'm also wondering if there's something I could append to the URL of my page that'll just show the source as opposed to rendering the page. Like this...
http://www.example.com/mypage.html#show_source
If you are okay with a link that opens source, you could use the following javascript:
if you are using FF:
window.location = "view-source:" + window.location.href;
and with IE:
var popup=window.open();
popup.document.open('text/plain').write(document.documentElement.outerHTML)
If all you need is code between the body tags - then you could do the following:
document.body.innerHTML
Can you provide a bit more information on the application?
"View Source Button" could be a solution, but if you need a direct link you need to change the Content-Type Header of your file to "plain/text".
If your page could be, for example, a PHP script, it would be:
<?php if($_GET["viewsource"]=="yes") header("Content-Type: plain/text"); ?>
and you can open link by appending ?viewsource=yes to your URL.
Remember, it works only "server side".