Keep referring URL after 404 page in php - 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

Related

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..

Url redirect with symbols on Wordpress with .htacces

I've got a URL with something like this:
www.google.com/?attachment_id=837
I want all the pages with an attachment to go to the homepage, for SEO reasons, I do not want those URLs to appear in a google search. I try to delete with Yaogast plugin, but I think the theme that is on the page is not coded "right".
I want to redirect to the home for example.
I tried something like this, and variations:
redirect 301 /?attachment_id=837 http://google.com
RewriteRule ?attachment_id(.*) / [R=301, L]
I think that "?" and "=" in the URL is giving me some problems.
The "?" indicates a GET variable. You cannot use URL Rewriting over GET variables. Changing your .htaccess will not help. I think the best thing to do here is to use the GET variable in the PHP file. Access your PHP file from wordpress, and add this code in the beginning:
if(isset($_GET['attachment_id'])) header('Location: redirect_url');
And replace "redirect_url" with the url you want to redirect to

Get the requested page

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']

.htaccess mod_rewrite to create clean URLs

I have a data driven site that passes information to determine what the next page should show using the $_GET parameter.
I want the URL's to look nicer and be structured simply.
I have been reading about mod_rewrite but so far failed to implement it.
<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>
This is taken from the URL to work out what table we want and what post ID. The URL at the moment is index.php?ID=4&loca=Pages
How would I make this work if it were instead. /pages/(the name column of the post of this ID).
This should do the internal rewrite:
RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages
It rewrites any url starting with pages/(some number)/ to the result. You should probably add some server side logic as well to do a 302 redirect if the url isn't exactly /pages/id/(Name that matches id)/. You can use $_SERVER['REQUEST_URI'] to get the string and then compare it to the string that it should be and do a redirect if it doesn't match.
Just like if you go to: https://stackoverflow.com/questions/11057691/This+is+not+the+title
You get redirected to the version with the correct title. You should also update the links you have around your site to use the new url format.
There are a lot of examples of how to do this on google.
Tutorial: http://wettone.com/code/clean-urls
Mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

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