Get the requested page - php

Is there any way with .htaccess to send to the 404 page also the requested page ?
so in other words let say I'm requesting a non existing page like :
domain.com/nonexistingpage.php
goes to
domain.com/404.html?requested=nonexistingpage.php
this is my current .htaccess line so it goes to 404.html
ErrorDocument 404 /404.html
I want to know what to add to get the results like this :
domain.com/404.html?requested=nonexistingpage.php
instead of :
domain.com/404.html

There's normally no need to do so. You can grab the requested page from PHP itself:
$_SERVER['REDIRECT_URL']
$_SERVER['REQUEST_URI']
I'd recommend the latter since it also contains the query string.

If your aim is just to get the requested url, you can use
$_SERVER['REQUEST_URI']
or
$_SERVER['REDIRECT_URL']

Related

Add URL Parameter To Htaccess Redirect ErrorDocument

On my website, when the user navigates to an invalid url I still want to display what he was looking for but when I redirect to my error.php via .htacces the faulty URL is lost.
I'm using this to redirect (.htaccess):
ErrorDocument 404 https://www.mywebsite.com/error.php
What I want insead is to add the faulty URL to the end of the redirect as URL parameter.
For examplewhen the user navigates to this URL https:www.mywebsite.com/this-does-not-exist I want that to reflect in my error.php as well and add the last part as URL paramater. The URL should then look like this:
https://www.mywebsite.com/error.php?url=this-does-not-exist
I already tried doing ErrorDocument 404 https://www.mywebsite.com/error.php?url=$1 and I also tried ErrorDocument 404 https://www.mywebsite.com/error.php [L] both versions did not give me the desired result.
How can I add the faulty url to the ErrorDocument redirect as url parameter in htaccess?
The easiest way is to use the document root and not the host in your .htaccess IE:
ErrorDocument 404 /error.php
Then in your PHP script error.php you can get the current URL simply by using
$url = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
echo "The page $url you are trying to reach doesn't exist!";
All the while KEEPING the current URL
https://www.mywebsite.com/this-does-not-exist
displayed in the URL bar.

Do not create an error page when a page does not exist

I have a domain which I want every attempted url after https://www.example.com/someFolder/ to not give an error but instead give a php page.
For example, I do not have a somefolder/abc.php file but going there will run a process.php file instead and display it there.
I have attempted to modify the 404 page as the process.php but that also modifies the example.com/ error page which I do not want.
It would be great if I do not need to modify/add a file at the root directory
PS. adding a .htaccess to the somefolder folder does work somewhat but then the url shows somefolder/404.php and not somefolder/abc.php
Modify your 404 page as you did putting php script there but check in php if the url that was requested was directory or not. Depending on that make an appropriate action
<?php
$url = $_SERVER[REQUEST_URI];
// check if requested url was ending with slash
$pattern = '#\/$#';
$result = preg_match($pattern, $url);
if ($result === 1) {
//request was to directory suffixed with slash
//do your php code for custom 404
die; //end the page generation
}
?>
request was not to directory ending with slash
put html of regular 404 page here or leave it blank
I have learned that I could turn somefolder into somefolder.php and use the $_SERVER['PATH_INFO'] to make all pages exist.
This is something that can only be done, in the general case, by using the .htaccess file, and redirecting every request like this...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,NC,L]
After that, you can use $_SERVER, $_SESSION, and other global variables in index.php to decide how to handle a 404 error (i.e., you can implement here how to define what a 404 is, etc.).
For instance, $_SERVER['PATH_INFO'] will be able to tell you what URL was requested, and you can use a combination of file_exists() and other calls, to determine if you want to display a full 404, search results, a redirect, or simply to display the results of another script.
This works for example.com/somefolder/, example.com/some/other/folder/, and example.com/a/b/c/d/e/f/g/, etc..

Keep referring URL after 404 page in php

I setup a 404 page and it works as it should. I am extracting the "error page" from the URL and using that to create a session since I need to pass it as a variable. My 404 has links to different pages and I would like to keep the referring url all the time even if the following pages exist. For example:
www.mysite.com/100
100 does not exist and goes to my 404. The url is shown as step 1.
Inside my 404 I have links to other pages likes www.mysite.com/link.php but I want that to be www.mysite.com/100/link.php
How can I accomplish this?
This is how I am passing the variable I need:
$page2 = $_SERVER['REDIRECT_URL'];
$str2 = substr($page2, 1);
session_start();
$_SESSION['DIST']=$str2;
Any help is appreciated!
You can use $_GET to pass a variable through the URL. Then rewrite
www.mysite.com/link.php?redirect_url=100
To
www.mysite.com/100/link.php
Using .htaccess
RewriteEngine on
RewriteRule ^([0-9]*)/link.php$ link.php?redirect_url=$1

how to Pass query string in 404 error redirect through .htaccess?

I have an index.php file which handles all requests to the server. I've set up a 404 error redirect in .htaccess in this way:
ErrorDocument 404 /index.php
If a users request a file, say, page.php, then it gets redirected to the index.php page properly.
The problem arises when the users request page.php?page=about, then my index.php is unable to retrieve the queryaboutusing$_REQUEST['page']`.
How can i get the query about through the index.php using $_REQUEST['page'] ?
$_SERVER['REDIRECT_QUERY_STRING'] may contain your query strings.
Give this a shot.
<?php
print_r($_SERVER['REDIRECT_QUERY_STRING']);
?>

how to redirect to 404 for a url of following format?

for some reason I see url of the following format in error_log (might have some sort of attack I not sure)
www.mysite.com/They-caught-my-eye/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/?p=2rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/rss/
Now what I want is to redirect this type of url to my 404.php page. I mean I want take into account of the number of url parts (number of occurances of /sometext/). If it exceeds lets say more than 10 I would like to redirect to 404 page. I know I can do this in PHP with parse_url() function , I am looking for a .htaccess way, will it be more helpful If I use .htaccess than through php file (my index file)?
Add into the .htaccess
ErrorDocument 404 http://www.yousite.com/404.php
Read it more here
Below are two ways you can make use of
this for 404
ErrorDocument 404 http://www.mysite.com/404.php
or this for ur desired page
RedirectMatch "\.html$" http://www.example.com/index.php

Categories