Using .htaccess to Handle Empty $_POST Requests

After searching extensively on the internet, I am still unable to find a solution to my problem. I have an .htaccess file that successfully rewrites my query string into the GET variable.

However, I have encountered a new issue when dealing with the POST variable for the first time. Although I have used this .htaccess file on multiple servers without any problems, it is now failing on one of my client's servers. Whenever I submit a form, the entire POST variable is empty. Below is the contents of my .htaccess file, any assistance will be greatly appreciated.

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^pages.*$ /error/404 [L]
RewriteRule ^classes.*$ /error/404 [L]
RewriteRule ^templates.*$ /error/404 [L]
RewriteRule ^common.*$ /error/404 [L]

RewriteRule ^([A-Za-z0-9-_]+)/?$ /?p=$1&s=index&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=index&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=index&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&%{QUERY_STRING} [L]
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ /?p=$1&s=$2&a=$3&n=$4&o=$5&%{QUERY_STRING} [L]

ErrorDocument 404 /error/404.php

The form I am using looks as follows:

        <div class="login">
            <form action="/administrator" method="post">
                <table cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                        <td>Username</td>
                        <td><input type="text" name="username" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="passwort" /></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td><input type="submit" value="Anmelden" /></td>
                    </tr>
                </table>
            </form>
        </div>

And here is the PHP code in the login file:

<?php
session_start();

if( isset($_SESSION['login']) && $_SESSION['login'] === TRUE ) {
    header('Location: /administrator');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $username = $_POST['username'];
    $passwort = $_POST['passwort'];

    $hostname = $_SERVER['HTTP_HOST'];
    $path     = dirname($_SERVER['PHP_SELF']);

    if ($username == 'xxx' && $passwort == 'xxx') {
        $_SESSION['login'] = true;
        header('Location: /administrator');
        exit;
    }
}
?>

Answer №1

Your RewriteRule is currently set up for GET requests only, not for POST requests.

  1. Make sure that your "/administrator" script (why didn't you name it "administrator.php" to begin with?) is being called or create a rule specifically for accessing it. Check the access log to confirm that the script is being called when you make a POST request to it.

  2. Consider adding something like:

    • RewriteRule ^administrator administrator.php [L]
    • or
      RewriteRule ^administrator pathtoyourscript/administrator.php [L]
  3. Check without using sessions to see if they are causing any issues with the rewrite rules.

You can use var_dump($_POST) to view all incoming data from the POST request. If nothing is displayed, it means that either the correct script is not being called or there is an issue with the path.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to send information containing the + symbol in an AJAX request?

I'm currently sending data to the server via an AJAX call using the code snippet below: var data = "productid="+productid+"&title="+title; $.ajax({ type: "POST", url: url + "target.php", data: data, dataType: "json"}) However, the title var ...

Retrieve the unique identifier for an SQL query in PHP

I have been utilizing the chart.js library to create graphs with individual ID's from users. However, now I am trying to modify my code so it can extract the user's ID from the URL and generate a graph based on that particular ID. Below is the c ...

Leveraging arrays for conditions in Laravel queries

Currently, I am utilizing Laravel 6 and would like to implement a time limit feature into my schedule. The limiting factors consist of an array with multiple string data. $bus = BusSchedule::where([ ['datetime', '>=', $sta ...

Can data be passed from PHP to AJAX and back again through an AJAX to PHP request?

I recently encountered a scenario where I needed to retrieve data from a SQL database using an AJAX request in PHP. However, the challenge was finding a way to send this information back to AJAX in variables rather than simply displaying it on the screen ...

using a loop to retrieve values from an object in PHP

I have a unique object that I received after printing $header echo "<pre>"; print_r($header); echo "</pre>"; stdClass Object ( [date] => [Subject] => [message_id] => [toaddress] => [to] => Array ...

Challenges with sending JSON encoded Arrays from WordPress to PHP results in empty or NULL responses

My Plugins site has multiple Inputs that I need to gather values from and push them into an array. Then, I utilize the jQuery.post method to send this data to my PHP script. Javascript: var json = JSON.stringify(output); // output is an array with multip ...

Setting up a WordPress application in a subdirectory alongside a basic PHP application can be easily achieved by configuring your .htaccess file with Apache

I am in need of help configuring a Wordpress application with a simple php application. The directory structure I am working with is as follows : Root directory : /var/www/demoApp/ Wordpress directory : /var/www/demoApp/wordpress/ My goal is to access t ...

After a post is saved on Wordpress, hooks can be used to trigger additional

After extensive research, I have come across a variety of answers but none that definitively answer my query. I am in need of running a function immediately after a post is saved to the database. This function must encompass all aspects of the post, inclu ...

To utilize the Fetch feature in React, login credentials such as a username and password are required

After spending countless hours researching, I still can't seem to find the answer I need. If this question has been asked before and I missed it, please feel free to redirect me to a helpful stack overflow page. Here is a brief overview of my issue. ...

I keep receiving data despite PHP showing a connection refused error

Something strange is happening here. I have a php server running on a remote location and a next.js app to interact with it. Everything was working perfectly, but now suddenly the login function is throwing a connection refused error along with the data. C ...

Issue with HTML While Utilizing wp_get_attachment_image

This script : $source = wp_get_attachment_image( get_the_ID(), 'medium' ); $weburl = wp_get_attachment_image_url( get_the_ID(), 'full' ); echo( '<p><a href="%s"><img src="%s"></a></p>', $weburl, ...

Retrieving data from MySQL through AJAX does not yield any information

I have been following a tutorial from W3 schools on PHP and AJAX database. Majority of the content is working fine, however it seems that there is no data being retrieved from the MySQL database I created called "exercises" in the "exercisedb" database. B ...

Simulating Stripe and AWS APIs within CodeIgniter

I have developed Integration tests for a website using CodeIgniter 2.x. The test suite combines Selenium with PhpUnit to ensure functionality. While testing, I utilized real Stripe and Amazon accounts within the application, raising concerns about creatin ...

Is it possible to retrieve multiple SQL statements within a single loop iteration in PHP?

Is it possible to query both SQL-SERVER and MYSQL databases in the same while loop in PHP, even though they have different database structures but produce the same result format? For example: $mssql_query = mssql_query(..mssql..); $mysql_query = mysql_qu ...

In Laravel with PHP, you can conclude the request by delivering a response based on specific conditions

After building a login backend, I realized the importance of checking specific conditions before allowing users to log in. However, I encountered a challenge where I needed to send different responses for each failed attempt. In the method below called in ...

After successfully implementing Laravel Login and Register functionality on the local environment, encountering difficulties when deploying it to the web platform

I've been troubleshooting why this issue is occurring. I set up a basic Laravel login using php artisan make:auth, and it works flawlessly on my local machine. However, after deploying my app online today, I encountered an error when trying to use the ...

Utilize AJAX and jQuery to seamlessly submit a form

I am attempting to use jQuery and AJAX to submit a form in order to add a row to a table called cadreSante (which is in French). The code I am using for this operation is provided below. Can someone please identify any errors in the code and suggest ways t ...

SQL and PHP employed in crafting navigation bars

Hey there! I have a couple of database tables: "category" (which contains category names) "sub_cats" (which holds sub categories) I'm attempting to display the sub categories for each category as follows: Category 1 Sub Category Sub Category ...

Submitting Forms with XMLHttpRequest

I'm trying to utilize XMLHttpRequest in order to retrieve the response from a remote PHP file and showcase it on my webpage. The issue I'm facing is that the form's data isn't being submitted to the remote PHP page. Any suggestions on ...

PHP - File_get_contents not recognizing CURL response

Here is the script I'm working on: <?php $auth_token = "privateprivateprivate"; $curl = curl_init("https://api-sandbox.coingate.com/v2/orders/"); $headers = array(); $headers[] = "Authorization: Token " .$auth_token; $output = curl_exec($curl); ...