I know there are many similar questions on this and I've read them and no they didn't work. I want to show 404 page when a non existing dynamic url is accessed on my website but without changing the url. For example:
https://www.trvme.com/destinations/corbett
is fine. But if I enter an invalid link like
https://www.trvme.com/destinations/corbetts
I get browser's 404 error but I don't see my 404 page.
Here's the code I have in PHP
if(isset($_GET['destId'])){
$link = mysqli_real_escape_string($connect, $_GET['destId']);
$thisPage = 'destinations/'.$link;
$query = "SELECT * FROM `destinations` WHERE `link` = '$link' AND `active` = 1 AND `delete` = 0";
$destinationsQuery = mysqli_query($connect, $query);
if(mysqli_num_rows($destinationsQuery)!=0){
// do stuff
} else {
http_response_code(404);
exit();
}
} else {
http_response_code(404);
exit();
}
And htaccess
RewriteEngine On
ErrorDocument 404 /message.php?id=2
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
I don't want to use header('location:message.php?id=2'); in php because that would change the URL. I'm getting 404 code from the URL but htaccess doesn't seem to be doing its job.
I also can't use
http_response_code(404);
include 'message.php';
because it throws all kinds of errors like session has started already and constants have been defined already. That doesn't seem like an elegant solution.
Edit:
The code in the linked question doesn't work for me. If I add the code above the destinations rule, the existing, legitimate pages also go to 404 because there's no actual file or directory, these are dynamic urls.
RewriteEngine On
ErrorDocument 404 /message.php?id=2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /message.php?id=2 [L,NC]
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
If I put it afterwards, it just doesn't work because the destinations rule takes over
RewriteEngine On
ErrorDocument 404 /message.php?id=2
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /message.php?id=2 [L,NC]
if you want a specific 404 you need to catch it or create the 404 HTML that is loaded within apache.
look at: catching all invalid URLs
They've suggested using the ErrorDocument within the apache.
Related
Please read the all. I know it is long but I really need help.
What I am trying to do is including the requested file in the "main-container", then rewrite the requested url, if it does not exists, show up 404 error page with .htaccess like ErrorDocument 404 /404.php
So, I know there is a lot of article about that things, but no matter I do, I could'nt succeed on this. There is always a problem. For example;
I tried to use .htaccess like that
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /page_handler.php
so every requests comes to page_handler.php first, it passes the requested page variable with $page to index.php, and index.php uses it to fill the page.
$page = $_SERVER['REQUEST_URI'];
if(empty($page) OR $page == '/') {
$page= "main";
}
include "index.php";
The problem with this one is, I can't use ErrorDocument 404 /404.php because whatever page is requested, it calls page_handler.php ,so the pages gets filled, no 404 error.
The second approach I tried is using .htaccess like that
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(admin|user)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
and in the index.php
global $pages;
$pages = #$_GET["page"];
$pages = explode("/", $pages);
and check $pages[0],[1] etc to calculate the page and include through it. With this one, you cant use
ErrorDocument 404 /404.php
so I tried to check if file exists, if not
header("HTTP/1.0 404 Not Found");
echo "<div class='content-wrapper'>";
include('404.php');
echo "</div>";
but it only simulates a 404 page, it does not send a real 404 error to bots and when I use it, you can call a page like www.example.com/articles/1/whateveryoutype like www.example.com/articles/1/1294ajgakshja1
it calls article with id 1 but what is that last part?
So as you see, I am a rookie and a LOST one. Don't know what to do. I tried a lot of thing, read a lot of webpage, tried much more things than these mentioned ones, but cannot solve my problem. I need someone to direct me to the right path. As I mentioned, I just want to fill the main-content area with the requested file, rewrite url to SEO friendly, and 404 the page if it does not exists. Thank you
I might have not understood your question properly.
I better use this:
.htaccess:
ErrorDocument 404 /404.php
404.php:
<?php
header("HTTP/2 404 Not Found"); // Or use HTTP/1.0 or HTTP 1.1
// Any other includes
echo "The requested URL ". parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH). " was not found on this server.";
// Any other includes
?>
While including, use the correct path from the / directory (if using linux)
/home/username/public_html/include.php
I have just started working with codeigniter, Working in admin control panel of my small project. Getting 404 page not found Error when i am trying to edit or delete.
In routes.php $route['admin/clients/update/(:any)'] = 'admin_clients/update/$1'; its giving me 404 Error.
If i access $route['admin/clients/update'] = 'admin_clients/update';, its loading edit form.If i pass segement 4 in url its giving me 404 page Not found Error.
here is my htaccess
RewriteEngine on
RewriteBase /testing_palace/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Am not getting what exact problem is.Please help me, response will be appreciated
You need to check the order of your route definitions.
For example
$route['admin/(:any)'] = 'admin/$1';
$route['admin/clients/(:any)'] = 'admin/clients/$1';
$route['admin/clients/update/(:any)'] = 'admin_clients/update/$1';
will always catch the first rule, and admin/clients/update/1 will look to admin/clients/update/1 and because of this, it may give a 404 error.
but like this:
$route['admin/clients/update/(:any)'] = 'admin_clients/update/$1';
$route['admin/clients/(:any)'] = 'admin/clients/$1';
$route['admin/(:any)'] = 'admin/$1';
every rule will be checked until a valid rule, so admin/clients/update/1 will redirect to admin_clients/update/1.
I have my query string to include my pages, in my homepage like you see below and it is working fine, Im including my pages fine.
But something wrong is happening and Im not finding how I can solve this.
I will try to expain my isse with an example: I have a folder "teachers" inside I have two pdf documents and a page "documents.php".
To acess this documents page, Im acessing: "htp://localhost/website/teachers/documents", and it is working fine.
But If I acess "htp://localhost/website/teachers/", Im able to acess my pdf documents and my page as you see in my image below.
But I dont want this, I want that If some user tries to acess "htp://localhost/website/teachers/", I want to include my 404 file (require_once('inc/404.php');)
My query string:
#$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);
if(file_exists('inc/'.$url[0].'.php')){
require_once('inc/'.$url[0].'.php');
}
elseif(file_exists($url[0].'/'.$url[1].'/'.$url[2].'.php')){
require_once($url[0].'/'.$url[1].'/'.$url[2].'.php');
}
elseif(#file_exists($url[0].'/'.$url[1].'.php')){
require_once($url[0].'/'.$url[1].'.php');
}
else{
require_once('inc/404.php');
}
Do you see what Im doing wrong to not be having the result I want?
My htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
There is an easier solution.
Add this line to the beginning of your .htaccess file:
Options -Indexes
This way, you won't be able to see the folder contents.
EDIT: htaccess rule solution (which is in website folder)
ErrorDocument 404 /website/inc/404.php
RewriteEngine On
RewriteBase /website/
RewriteRule ^teachers/$ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
This is the real url:
http://web/app/index.php?id=1
Here I have used current .htaccess and working fine with me.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
Now the url is : http://web.com/app/index/i and working fine
but here is the problem the real url
http://web.com/app/index.php?id=1&name=abc
How can I set mention url with .htaccess for keep it short or any other good solution for hiding URL-.
is it possible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.
If we say, good practice is to rewrite everything to one index page, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :
http://example.com/app -> $uri = /app
http://example.com/app/sub-app -> $uri = /app/sub-app
but also query string applies to the $uri variable:
http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
in this case, if you want to examine just uri part before ? question mark:
$realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
next you can examine and manipulate $uri;
Example:
$trimmedUri = trim($realRri, '/');
if ($trimmedUri == 'app')
{
// you can show app page
}
elseif ($trimmedUri == 'contact')
{
// show contact
}
else
{
// show 404 page not found
}
// you can have some dynamic also also
if (is_file($filePath = 'custom-page/'.$trimmedUri))
{
include $filePath;
}
else
{
// 404 page
}
Final Code (regarding your current script)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#rewrite php file
RewriteRule ^([a-zA-Z]+)$ $1.php
#rwwrite with id
RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
#rewrite with id and name
RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3
I think what you're looking for is something like AJAX, mostly because of
but it could go to other pages while the url stay one url static.
in which case, you would looking at JavaScript and not php. You would need a way to quickly deal with the data and change it how you want it. I've recently started learning Angular, and it shows a lot of promise. Go through the tutorial yourself (just google angular.js and the tutorial is on their website), or some other JavaScript library (unless you want to hard-code everything, in which case, good luck) like jQuery or MooTools (names mentioned because they're libraries i have knowledge with). To my knowledge (I am no code guru) but a website's URI is what tells the server what to show you. You can make shortcuts
If, however, you want for it to just not have the .php filename, then I believe that
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.php\??(.*)$ $1.php&$2
Should, theoretically get you what you want.
Note: I can't test it right now, so it may error out.
EDIT: It does error out.
I have read lots of threads about htaccess redirect, but none really match my situation, albeit quite simple, got me pulling my hair out.
for example I have a site with this structure:
root
-gallery.php
And I have already implement htaccess rule to hide .php extension.
How do I catch the following events:
mysite.com/nonexistingfolder would go to mysite.com/404.php
mysite.com/gallery/nonexistingfolder would still load mysite.com/gallery, so that I can catch the /nonexistingfolder as a $_GET inside mysite.com/gallery.php?
Append this after RewriteRule
ErrorDocument 404 /error_docs/404
Replace /error_docs/404 with the file that shows the error or does something.
For the regular 404 page, use an error document:
ErrorDocument 404 /404.php
That covers #1, non-existing folder will go there.
For #2 try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)$ /gallery.php?page=$1 [L]