Block direct access to PHP files - php

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.

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.

Dynamic .htaccess 404

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?

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.

.htaccess referencing images from subdirectories

I'll try to make this brief. This is my first attempt using HTACCESS to create "user-friendly URLs". I've set up my .htaccess file so that when a user clicks a link that appears to be in a subfolder, it loads a file within the root.
ex:
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^current-communities/(.*)$ $1 [L]
So if I click a link like "/current-communities/my-community it will load the file "my-community.php". No problem there.
However, now I'm trying to make another rule that when I click a "floor-plans" link, the floor-plans.php file will load data from a database based on its querystring:
RewriteRule ^floor-plans/([^/]+)-([^/]+).php floor-plans.php?communityname=$1&modelname=$2 [NC]
So if a user clicks on a link like "/floor-plans/my-community-myhouse", it will load the "floor-plans.php" file, with "my-community" as the first querystring variable, and "myhouse" as the second.
The issue I'm having here is that the "floor-plans.php" file is not showing the pictures. It is looking into a folder called "images" for graphics, but because "images" is actually within the root directory, I only get a very empty page, but for some reason this is not the issue with the virtual directory of "current-communities".
Can you help?
Hello try using the next code in the head of your document, basically what it does is to set path for all scripts and css files to the actual root of the php document and not what the address in the browser is set up to your using.
so what it actually does is once set in the head before importing anything else php will automatically set the path if you give on css/style.css, php will set it before it goes back to the browser to http://www.myweb.com/css/style.css
it works fine for my wbesites hope it helps you.
<BASE href="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['PHP_SELF'] ?>">

after .htaccess url rewrite, cannot perform logoff in some of the url rewrited page

Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.

Categories