Mod_Rewrite Extension Removal Changes Functioning of 404 - php

When I try to remove my PHP extensions from all my files using my .htaccess file on my Apache server, everything works great. The extensions are removed, and everything looks much better.
However, I'm having one small issue: when I would normally go to a page such as ./nonexistent.php, I would get a 404 error. But, when I rewrite my URLs, and I go to ./nonexistent, I instead get a 500 Internal Server Error.
Ideally, I would like to simply redirect my user to a custom 'Page Not Found' page, but I can't currently find a way to do this.
Here's the code I'm using:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
I've tried setting: ErrorDocument 500 /nope, however, this doesn't seem to have an effect either.
So, to conclude, does anyone know how to rewrite extensions, while maintaining the same functioning of the 'Page Not Found' system that is the default?

When you are requesting a non-existent file using the above rewrite conditions, you are running into an infinite redirect.
If you access http://yoursite.com/i-dont-exist, the first condition is evaluating true, i-dont-exist is a non existent file, so it will try to rewrite to i-dont-exist.php which also doesn't exist so the rewrite pattern continues until Apache limits the recursion and gives you a 500 error (essentially it is continually rewritten to i-dont-exist.php.php.php.php.php...php until you encounter the 500 error.
You can resolve this by adding an additional check to make sure the file with the .php extension exists before rewriting.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f #make sure $1.php exists before rewriting
RewriteRule ^(.*)$ $1.php
If the file.php exists, then it will rewrite to it, otherwise it will not, and the 404 error page will be served.

http://www.openia.com/blogs/errordocument-and-mod_rewrite-workaround

Related

Rewrite rule to hide extension .htaccess

I am trying to hide the extension of .php file from the link
for example www.example.com/about.php
To display www.example.com/about
what I did
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
and It works perfectly.
but I have another link which example.com/news.php?id=45
according to the above rule, I can access the link like
example.com/news?id=45 without .php
but I want to hide id=45 I want it like this example.com/news/45
what I did RewriteRule ^news.php?id=([0-9]+) /news/$1 [NC,L]
But It won't work I got 500 Internal Server Error
Try it like this instead:
# MultiViews must be disabled for "/news/45" to work
Options -MultiViews
RewriteEngine on
# Rewrite "/news/45" to "news.php?id=45"
RewriteRule ^news/(\d+)$ news.php?id=$1 [L]
# Handle extensionless ".php" URLs
RewriteCond %{REQUEST_URI} !\.\w{2,3}$
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
Your 500 Internal Server Error was actually caused by your original directives that "hide" the .php extension, not your "new" directive (that wasn't actually doing anything). Your original directives would have rewritten a request for /news/45 to /news/45.php to /news/45.php.php etc. creating a rewrite-loop (500 error).
See my answer to the following question on ServerFault with a detailed explanation of this behaviour: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
what I did RewriteRule ^news.php?id=([0-9]+) /news/$1 [NC,L]
But It won't work I got 500 Internal Server Error
The logic of this directive is reversed and would never actually match the requested URL (or anything for that matter), so won't actually do anything. It is, however, syntactically OK, so won't trigger an error.
The 500 error would have occurred simply by requesting /news/45 (with your original directives), whether this directive was in place or not.

Can't access filename.php without .php in extension

The code inside my .htaccess file is:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Is this code all right?
I was told that this would ensure that I can access my files without typing .php in the browser. However, this is not the case. I'm using WampServer Version 2.5. I've already ensured that rewrite_module is turned on in the apache modules, and ensured it's on in httpd.conf. My port 80 is used by apache itself.
So why can't I access the page without typing the extension? I get a 404 Error when I try to do so.
EDIT : I tried the solution provided Here, with the new code being:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
But it still returns a 404 Error. Why is this happening?
NOT FOUND. The requested URL /SomuFinanceAJAX/bill/billMaker/index was not found on this server.
As per the comments, the issue appears to be related to the RewriteBase. Remove the RewriteBase / altogether.

.htaccess remove extension but allow 404 if not existing

I have removed file extensions on .php for my webserver. I just wanted to make it look nicer. This works excellent.
I have the following pages:
sitename.com/portfolio
sitename.com/portfolio/resume/file.html
The htaccess has rules so that when they visit the second link its just converting it to portfolio.php?resume&file=$1. That works awesome.
Okay heres the issue. Because i remove the extensions, if a user were to typ the domain as sitename.com/ portfolio/ with a traing slash, they will get an internal server error. Likewise, if they just type sitename.com/portfolio/r they also get an error since it doesnt exist. I know one way around this is to add an option in htacess for every page but that seems like a bad way of doing it.
Anyways here the current htacess file I am using:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^portfolio/resumes/([^/]+).html$ /portfolio.php?resume&file=$1 [L,QSA]
RewriteRule ^portfolio/resumes$ /portfolio.php?resumes [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
ErrorDocument 404 /404.php
If i visit the site on a page that doesnt exist like sitename.com/1 or sitename.com/1.php then I get my 404 page, but if 1.php DID exist then i would see it and IF it did exist, and I typed sitename.com/1/ or sitename.com/1/somethingelse then instead of a 404 (which I expect) then it gives me an internal server error. Is there any way around this? an error in my syntax? Thanks
This is because when you request an uri with a trailing slash eg : /uri/ , your rule rewrites it to /uri/.php instead of /uri.phpwhich is an unknown/unsupported destination. You need to accept the trailing slash as an optional char. Change your rule to :
RewriteRule ^(.*?)/?$ /$1.php [L]

htaccess causes 500 if page doesn't exist

I am currently using this code to remove the .php extension of my files on my apache web server.
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
two questions:
1: when someone requests a page that doesn't exist, my web server is returning a 500 error instead of a 404. how can I fix this?
2: how can I force a 404 if someone requests the .php extension?
thank you in advance.
The 500 error for non-existent pages is happening because mod_rewrite is going into an infinite loop trying to rewrite your request and terminates eventually.
Rewrite the rules like this to make sure the file with the PHP extension actually exists:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
The RewriteCond %{REQUEST_FILENAME}.php -f line will cause the rewrite to take place only if "file.php" exists.
example.com/about-us.php to go to example.com/about-us.html
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php
This is not complete answer.
I try to explain how RewriteEngine works, if you don't understand that htaccess feature completely.
Apache server 'receives' some 'not real' url from your browser. Then it reads .htaccess, and by using information from that file it converts that 'not real' url to 'real' url pointing to physical file on server.
Error details are always saved to apache (httpd) error log. You have to read it, and if it not tells you anything - paste it here - we will help.
Forcing 404 error is bad idea in this case. You should fix error, not override it.

Strange htaccess Problem

I have a website with this in the .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L,QSA]
#DirectoryIndex index.php
My problem is even, if I change a single letter the website is rendering a 500 Error. Even if I empty complete file, it still shows me the 500 Error.
What I want to do is, there is a page like this on the domain
http://www.example.co.uk/brochure/generate.php
The file generate.php does exist in /brochure/ directory.
But still the generate.php does not load and it is loading the index.php file.
Any help?
Try the following which I know should work:
<IfModule mod_rewrite.c>
#Turn the Rewrite Engine ON
RewriteEngine On
#Set the base where to rewrite the requests
RewriteBase /
#Allow direct file access
RewriteCond %{REQUEST_FILENAME} !-f
#Allow direct directory access
RewriteCond %{REQUEST_FILENAME} !-d
#Disallow certain files.
RewriteCond %{REQUEST_FILENAME} !brochure\/generate\.php$
#Rewrite URLS To GET[menu] and make sure this is the last rule.
RewriteRule ^(.*)$ index.php?menu=$1 [L]
</IfModule>
if you do not see any rewriting taking palce then check to see the module for rewrite is installed.
If an empty file triggers a 500 status code as well, then the error is somewhere else: you are not editing the real .htaccess file being used by Apache, the error comes from the application, etc.
In any case, you should find the error log: that's where the exact details are shown. In Linux, it's normally under /var/log/httpd. In Windows, it can be under %ProgramFiles%\Apache Software Foundation\Apache2.2\logs. If it's a shared hosting account, you'll probably have an "Errors" or "Logs" feature in your control panel.

Categories