htaccess rewrite server error - .htaccess

Hi guys experimenting with .htaccess with XAMPP on my localhost, I have checked with phpinfo() if mod_rewrite is loaded and it is, so that shouldnt be the problem..
This is my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA, L]
and this is the error it gives on every page
SERVER ERROR
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
Whats wrong with my htaccess or XAMPP? :)

$url was empty, make sure you echo out !

Related

Rewrite url with query parameter

I would like to rewrite a specific url and remove the query parameter. However online I can't seem to find a solution which works for me. I'm pretty new to .htacces files and the working of them.
My file for now, which is in the main folder of my website:
ErrorDocument 404 /404.html
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
#Remove id query parameter
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1
So the normal url is: https://example.com/api/webhook.php?id=10
which I want rewrite as: https://example.com/api/webhook/10
Does anyone know how to create this for this specific case? Thanks in advance!
EDIT:
In my webhook.php file I have this for testing purposes:
<?php echo $_GET['id']; ?>
When I go to the url https://example.com/api/webhook.php?id=10 I get output 10 in the browser.
When I go to the url https://example.com/api/webhook/10
I get this error message:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
And in the serverlogs I found this error:
Request exceeded the limit of 4 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace
Seems there are too many rewrites.
SOLVED
I found that these lines fixed my problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/webhook/(.+)$ api/webhook.php?id=$1 [L]
Now when I go to https://examply.com/api/webhook/10 it loads the webhook.php script with query ?id=10. The page now outputs '10'.
RewriteEngine On
RewriteRule ^api/webhook/([0-9]+)/?$ api/webhook.php?id=$1 [NC,L,QSA]
Usage = These rules will make your url https://example.com/api/webhook.php?id=10 to https://example.com/api/webhook/10

.htaccess mod_rewrite 500 Internal Server Error

I succeeded to use .htaccess with mod_rewrite for my first domain name.
I try to use it for a second domaine name, and I have for result "Internal Server Error".
However, I put the same files on their server, and they have the same configuration.
The content of the .htaccess file :
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Where can the problem come from ?
Best regards,
I found the solution, it's a problem with the encodage.
Getting Error 500 with this .htaccess on Linux, works fine on Windows

WAMP - Trailing Slash (/) Gives Error 500 "Internal Server Error" - .htaccess Error?

I use WAMP on my computer and when coding my website I decided to remove the .html extension in hopes of improving aesthetics.
This works fine, so, in my case, temp.jakegriffin.co.uk/index.html became /index which is what I wanted. Unfortunately, if there is a trailing / after index, I get:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, admin#localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
When I attempt to fix this in the .htaccess file, I either get the error 403 were I'm forbidden or the error 404 were the page does not exist.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
That is the code in my .htaccess file for the removal of the extension. If someone could help me, it would be greatly appreciated.
Thanks in advance,
Jake.
This this mod_rewrite rule:
* http://planetozh.com/blog/2004/05/apache-and-the-trailing-slash-problem/
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)[\/*]$ $1.html [L,NC]
now you can add a bunch of forward slashes (/) after index ;)

Expression Engine hide index.php not working correctly

I am working on a site that was working before someone else made some changes to the site. Now, I have to include index.php to the URL before the contact form will work.
Here is the htaccess file code I am using:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
What am I missing here?
Thanks.
The ? after index.php indicates to me that you're running PHP as CGI ... perhaps that was changed?
Also see: https://stackoverflow.com/a/11143216/174299

.htaccess Check for file in two locations, otherwise throw 404

The way my site is currently structured, the actual site is in its own separate folder. Here's what I mean:
/projects
/files
/pictures
/tools
/school
/~webroot
.htaccess
This makes the file-system much easier to manage and navigate. An easy way to utilize this, without having everyone navigate to http://domain.com/~webroot/, and still allow them to access files and such like http://domain.com/projects/, is to use the htaccess I wrote below to check for files in both the real root, and ~webroot directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
However, if the file doesn't exist anywhere (root or ~webroot), an HTTP 500 error is thrown instead of an HTTP 404. In order to display my 404 error page, I have to instead use these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /404.shtml [B,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^/~webroot
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
This is all quite messy, and it's only a trick and doesn't actually throw an HTTP 404, which keeps 404s from being documented using my statistics application. Is there a way to: Check if file exists in root, if not check if file exists in ~webroot, if not throw real HTTP 404 error?
I do also have all my ErrorDocument's defined properly.
I know this was a while ago, but wanted to chime in. For a rewrite rule to hit a 404, I always use this:
RewriteRule ^(.*)$ - [R=404,L]
I'm not sure if it's correct, but it works for me. I'm pretty sure it should always be with "L" to prevent further rewriting.
BTW, what is the "B" flag for? I only know the ones listed on the apache mod_rewrite page, so I'm lost on it's meaning.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
EDIT: ah, my problem is I look at older version docs. Thank you, Swivelgames. I probably would have never even known I was looking at the older docs if you hadn't pointed that out (need to update my bookmarks).
http://httpd.apache.org/docs/2.3/rewrite/flags.html#flag_b
If you can make your 404 page a PHP file, you can add this before any output to get real 404's:
<?php header("HTTP/1.1 404 Not Found"); ?>
This will cause PHP to trigger a 'real' 404 (from the browsers point of view) which gets passed back to the browser. If your statistics package is internal to your server this approach may or may not work, I'm not sure.
The only other solution I can think of is to redirect to a non-existent file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/non-existent-404-generator
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /non-existent-404-generator [B,L]

Resources