Dynamic .htaccess 404 - php

Currently, about half of our webpages derive from our HTML_page.php.
I want to add a <div class="Error404"> tag, 404 div, there to display 404 error information.
If a non-HTML_page.php webpages triggers a 404 error, I cannot display an error message using my 404 div.
UPDATE: A non-HTML_page.php would be any of our older, legacy pages that do not yet inherit from our "HTML_page.php" base class and all external websites that link to items on our domain that may not exist.
So, the default structure still needs the 404 routed to our 404.php page, but modify the routing if I can detect that the webpage contains my 404 div.
Here is how our .htaccess page starts out now:
# Custom 404 page
ErrorDocument 404 /eForms/404Form.php
#
# Turn on rewrite engine
RewriteEngine on
#
# Rewrite rule for document secuity
RewriteRule ^(.+)\.pdf$ /docgateway.php?source=$1.pdf [L,NC,QSA]
This snippet of jQuery's hasClass illustrates the logic I want:
if ($("div").hasClass("Error404")) {
Is there any way to build that type of logic into the .htaccess file for the ErrorDocument?

Related

.htaccess file redirect pages that start with the same string

I am trying to redirect two pages and i am using thw following code
RewriteRule projects/(.+) /Pages/Shop/SinglePortfolio.php?ID=$1 [NC,L]
RewriteRule projects$ /Pages/Shop/Portfolio.php [NC,L]
The first page will be like /projects/project-no-one
The second will be the /projects
This rule is currenty shows the Portfolio.php page, but not the SinglePortfolio.php (404)
If i revert the rules shows the SinglePortfolio.php page, but not the SinglePortfolio.php (404)
Tried
Without the $ at projects$
Other regular expressions
changing the projects/(.+) to project/(.+) but the prefix is the same and that seems to cause a problem.

How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

I have a page (example.com) with anchors at major sections (example.com/index.php#anchor, example.com/index.php#about, example.com/index.php#contact, etc.) as well as subpages (example.com/subpage.php).
Here's my .htaccess file:
RewriteEngine On
DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php
RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]
The subpage rewrite rule works great.
For the anchor link, including the "R" (redirect) flag changes the URL to example.com/index.php#anchor, but I want the URL to be example.com/anchor.
If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor.
So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript?
It's not possible to do that with URL rewrite. Because the browser scroll down when the # is part of the URL... not on the server side.
You can't do this with .htaccess. But you can do it with the following JavaScript code:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

Issue with # in URL with a Wordpress theme?

I am having a severe problem and have no clue about what is going on... I will specify the general issue which is causing multiple issues on this Wordpress powered portal.
Steps to reproduce:
Visit the URL: http://gamersgeographic.com/en/ (or any post on this site)
Append #abc or #anything to the URL
The URL tries to resolve for a second and magically deletes the "#" and instead changes to /abc or /anything , which of course does not exist and gives a 404 page not found.
Even if the local anchor with #abc exists, behaviour is the same.
Now, consider the case below:
Visit http://gamersgeographic.com/monster-hunter-diary-1/
Comment link appends a #comments or #respond depending on whether a comment is there or not.
Both the anchors exist on the single post page
Still it redirects after finding them, to /comments and gives 404
Direct URL with #comments works e.g. http://gamersgeographic.com/monster-hunter-diary-1/#comments works but when I change any base URL to #comments, it redirects to 404...
I have tried several combinations with Permalinks, so it is not a problem with that. I even wrote my own Comment link generator in php with just a plain
href="#comments"
but still no luck...
If you need any further information about any function's code in theloop.php or anything please let me know.
Thanks in advance !
Regards
The contents of .htaccess are as below:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This is not a PHP issue, it is Javascript: it is evident when you reproduce it, and you can test it by disabling Javascript and adding #comments at the end of the URL; it will work.
Now, I have done some work for you, and the culprit is a Javascript file aptly named hashchange.js. Look, for example, at this line:
function second_passed() {
if(current_page!=location.href {
get_page_by_hash(location.href);
}
setTimeout(second_passed,1000);
}
Which explains why you see it “working” for a second.
And here is the redirect:
jQuery(window).hashchange(function() {
var link = window.location.hash.replace("#", "");
get_page_by_hash(link)
});
Note that hashchange is a method for event handling available in jQuery Mobile.
<link rel="canonical" href="URL OF YOUR HOMEPAGE HERE>
add this in your header.php in <head></head> section and then try . it shouldn't be giving 404 error !
The way that page bookmarks are used is, as you know, the href="" of an anchor points to an #some-place. In order for this to happen #some-place must be the id of the element within the page you wish to go to.
For example:
http://gamersgeographic.com/monster-hunter-diary-1/#respond
should take you to the element with id="respond" in that page.
If the element with that ID doesn't exist you won't be able to travel to it, and may be the reason it results in a 404 Not Found. However, if the element does indeed exist on the page with the proper ID and it still redirects to a 404 then you may want to check your web server configuration to make sure it isn't filtering the # in some way.

Passing URL entered by user to 404 page with Htaccess and PHP

I have a website with PHP and htaccess support.
Basically, I want to pass the URL entered by the user (that caused the 404) to the 404 page itself.
Is there any way to do this?
Add following rule in your .htaccess file.
ErrorDocument 404 /error.php
In the error.php file.
<?php // error.php
echo $_SERVER["REQUEST_URI"]. " doesn't exists. Sorry!";
?>
Just add the following in your .htaccess file (where 404.php is your error page)
ErrorDocument 404 /404.php
You can do it by adding the following rule in .htaccess.
ErrorDocument 404 /404.php
In your 404.php you can write
<h3>
Page Not Found.
</h3>
<div>
<p>Apologies, but the page you requested could not be found.</p>
<ul>
<li>You may have mistyped the page address.</li>
<li>You may have clicked on a link that has not been updated.</li>
<li>We have recently changed our site structure and the page has been moved to new location.</li>
</ul>
</div>
You can style this to display it properly.
Hope this helps :)

Block direct access to PHP files

My present htaccess file contains mod_rewrite rules to perform this:
www.mysite.com/articles/jkr/harrypotter --> www.mysite.com/index.php?p=articles&author=jkr&book=harrypotter
www.mysite.com/articles/jkr --> www.mysite.com/index.php?p=articles&author=jkr
www.mysite.com/articles --> www.mysite.com/index.php?p=articles
In my root directory, I have some PHP files like: index.php, pre.php, view.php, etc.
What I want: block direct access to php files.
Eg:
www.mysite.com/index.php --> "404 - Page Not Found" or "File not exist"
www.mysite.com/view.php --> "404 - Page Not Found" or "File not exist"
I have tried a code that I found on searching. It uses "f" flag. But i did not understand it. When I used it do not show the home page also. So, I removed everything that I was testing. My htaccess file now contains three rules only(to accept page, author, book).
Please help.
I saw another question that was previous asked in here : mod_rewrite error 404 if .php
A change that I made to it is, added [NC] flag. Otherwise, if we put the uppercase letter in extension(say, "index.PHP") it will load.
So,
SOLUTION:
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
thank you
Create a simple 404.php that throws a 404 error, then rewrite index.php to 404.php etc.
You can also do this at your script level. If you add a rule within your index.php to check whether you have been passed GET variables or just called the page without any parameters like so:
<?
if(empty($_GET))
header('Location: http://www.yoursite.com/404.php');
else
//your usual script
?>
Then you have control to either redirect people to a 404 error or show something more helpful, like a landing page with an index or some form of website navigation.

Categories