htaccess causes 500 if page doesn't exist - php

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.

Related

.htaccess ReWriteRule - Requested URL <rewritten filename> was not found on this server

I've been testing this in WAMP and I can't get it to work. I believe WAMP is set up properly due to the error message I'm receiving and it works no problem when I don't use the .htaccess file.
I have a filename that I'm using for testing called Feedback.php. Instead of displaying as www.mysite.com/Feedback.php. I'm trying to get it to just be www.mysite.com/Feedback.
.htaccess file
RewriteEngine on
RewriteRule ^Feedback.php/?$ Feedback [NC]
The error message that I receive is
"Not FoundThe requested URL /Feedback was not found on this server."
Are there two files required for reWriteRule to work?
I'm also trying to get this to work for my index.php to just be www.mysite.com which may be a different monster.
What should my navbar links be? Right now they are Feedback Should href perhaps be href="Feedback" once I get this working?
EDIT: I had the ReWriteRule variables backwards. Instead of
RewriteRule ^Feedback.php/?$ Feedback [NC]
I should have
RewriteRule ^Feedback/$ /Feedback.php [NC,L]
The rewrite doesn't look to have taken place though as it still reads localhost/Feedback.php
You may use this to remove .php extension from your pages as well as omitting the index part to your website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index
RewriteRule ^index$ http://yourwebsite.com/ [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

changing .htaccess to skip .php from URL results in error for some web pages

I setup .htaccess file as follows to make elegant url by hiding .php extension;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now all of my pages work other than following three urls/pages;
www.auroracs.lk/about
www.auroracs.lk/enquiry
www.auroracs.lk/locate
for above URLS I get following error. (You may check online by trying that URL right now)
..............................
Not Found. The requested URL /about/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
..............................
But if I rename about.php to about1.php then www.auroracs.lk/about1 url works. same happens to other two php files also. Can any one help why its not working only for these three pages. Is it because my .htaccess conflicting with some other file or any advice is appreciated.
Replace your rule by this more correct rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteRule to redirect to php file not working

I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.

Mod_Rewrite Extension Removal Changes Functioning of 404

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

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