I have a MediaWiki 1.32.0 website. MediaWiki website directories contains the file LocalSettings.php in which one could write global custom PHP.
As a non PHP programmer I ask if there is some PHP command I could use to restrict access to a certain existing webpage, by that webpage's URL, so that any web request to create it('s HTML) would be denied, either resulting in some 404-like HTTP status code, or a redirection to homepage, as long as that command appears in LocalSettings.php?
I would prefer a PHP way instead an Apache PCRE directive in .htacess. Also, I should note that the URL is already blocked by robots.txt.
You can get the requested URL in PHP like this:
$_SERVER['REQUEST_URI'];
And deny access to a page like this:
$url = $_SERVER['REQUEST_URI'];
if ($url == "/wiki/blocked_page") die('404 error here');
Or redirect to your home page like this:
$url = $_SERVER['REQUEST_URI'];
if ($url == "/wiki/blocked_page") header('Location: /');
Note: request_url is everything after your domain name.
If your URL is https://example.com/wiki/blocked_page request_url would return "/wiki/blocked_page".
Related
Let's say I have a application hosted on www.example.com, I did a seo check it says that www.example.com redirects to www.exmaple.com
Also to double check i used the below code to check some input params
$page = explode("/", $_SERVER['REQUEST_URI'])[1];
echo $page;
When I ran the above code on url www.example.com/about it returned about about twice also when I ran the same code on www.example.com/some-url-here it returned some-url-heresome-url-here what's the catch? why is the application functioning like this.
I have a subdomain which I want to forward to a changing url for post requests.
The changing url can be obtained using a php script I have.
Example:
My subdomain:
subdomain.website.com
Php script:
website.com/getsite.php
I want the "subdomain.website.com" to redirect to whatever url is in the php script (it changes frequently), all this while forwarding post requests back and forth.
How can I achieve this? NodeJs? Php script? I am at a loss on how.
use $_SERVER ['HTTP_HOST']. It is your domain/subdomain.
if ($_SERVER ['HTTP_HOST'] == 'subdomain.website.com') {
Header ("Location: http://website.com/getsite.php");
exit();
}
What I want is rather simple but I cannot figure it out. I want that if someone tries to access any of the webpages directly on my website then he/she must be redirected to the home page of the website.
For Example : Someone tries to access www.domain-name.com/aboutus he gets re directed to www.domain-name.com/
you can check referrer to block any direct access to your website.
include this code in every page of your website.
<?php
$ref = $_SERVER['HTTP_REFERER'];
$url = "http://www.domain-name.com";
// $url is the main url of your website
if($ref==$url){}else{
header("location:$url");
}
?>
But it is completely deepened on browser to send the referrer header,
so it may not work sometimes.
You can use 2 way do it:
In your code, directed www.domain-name.com/aboutus to www.domain-name.com
Config webserver (apache, nginx)
When I try to redirect page using the header('Location: ...') function it is appending the redirect URL to current pages URL instead.
For Example, my current page is http://www.example1.com.
I have created a simple file called test.php with the code below in it:
header('Location: http://www.example2.com');
However it is currently redirecting the page to http://www.example1.com/http://www.example2.com instead of http://www.example2.com.
Any suggestion what should be the issue?
This issue has arisen after moving the site to a new server. I am running the site by doing host entry.
I was wondering if it's possible to take a url request from an external server, process, and then return to the requester a different url. (specifically a media file)
For example: www.example.com/index.php?var1=blue&var2=green
I'd like to be able to use that url to access a media file hosted on the example.com server. I don't have access to code on the requesting site, so my php site index.php will need to take that url request and process based on the get vars, and the correct media file will be presented to the external site.
How about doing a redirect using header:
if (isset($_GET["var1"]) && $_GET["var1"] === "blue"){
header('Location: YOUR_BLUE_CONDITION_URL');
}else if (isset($_GET["var2"]) && $_GET["var2"] === "green"){
header('Location: YOUR_GREEN_CONDITION_URL');
}
As #Fred noted below, make sure you do not output anything prior to modifying the headers.