14 Replies - 1368 Views - Last Post: 04 May 2015 - 11:36 PM

#1 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

.htaccess not allowing me to submit forms

Posted 02 May 2015 - 09:12 PM

Today I added a .htaccess file and took the .php names off of everything. Now I am unable to post most forms on my site. This is ranging as far as adding products to the shopping cart, deleting records, sending a contact form. I saw a couple of problems like this while searching, but nothing I tried helped for my case.



Here is my htaccess file. The bolded text is what I tried adding to resolve this.
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^er$ er.png [L]

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>
[b]
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC][/b]



For a source of reference. Here is part of my contact page/form to get an idea. This worked flawlessly before I added in the .htaccess file and so did the other pages I mentioned.

<?php

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['number']) && isset($_POST['message']) && isset($_POST['human'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
	$human = $_POST['human'];
	
	if (!empty($name) && !empty($email) && !empty($number) && !empty($message) && !empty($human)) {
		if (strlen($name)>50 || strlen($email)>50 || strlen($number)>10 || strlen($message)>1000) {
			echo 'Sorry, the content you typed in was too long.';
		} else {
		$to = '[email protected]';
		$subject = 'Contact form submitted.';
		$body = $name."\n".$number."\n".$message;
		$headers = 'From: '.$email;
		
		
		if ($human == '4') {
			if (mail($to, $subject, $body, $headers)) {
				echo 'Thanks for contacting us. We\'ll contact you shortly.';
			} else {
				echo 'Sorry, an error occurred. Please try again.';
			} 
		} else if($_POST['submit'] && $human != '4') {
			echo 'Sorry, you answered the anti-span question incorrectly.';
		}
			}	
		} else {
			echo "All fields are required";
		}
	}
?>


				<script type="text/javascript">
				function validateForm()
				{
				var a=document.forms["Contact Form"]["name"].value;
				var b=document.forms["Contact Form"]["email"].value;
				var c=document.forms["Contact Form"]["number"].value;
				var d=document.forms["Contact Form"]["message"].value;
				var e=document.forms["Contact Form"]["human"].value;
				if (a==null || a=="" || b==null || b=="" || c==null || c=="" || d==null || d=="" || e==null || e=="")
				  {
				  alert("Please Fill All Required Fields");
				  return false;
				  }
				}
				</script>


				<div id="contactenvelope">
					<form id="contactform" name="Contact Form" action="/contact" method="POST" onsubmit="return validateForm()">
						<label>Name</label>
							<input name="name" required="" type="text" class="inputbar" maxlength="50">
						<label>Email</label>
							<input name="email" type="text" class="inputbar" maxlength="50" required>
						<label>Contact Number</label>
							<input name="number" type="text" class="inputbar" maxlength="10" required>
						<label>Message</label>
							<textarea cols="15" name="message" class="messageinput" rows="5" maxlength="1000" required></textarea>
						<label>*What is 2+2? (Anti-spam)</label>
							<input name="human" placeholder="Answer Required" type="text" class="inputbar" required>
						
							<input id="newssubmit" name="submit" type="submit" value="Send Message">
					</form>
				</div>


Is This A Good Question/Topic? 0
  • +

Replies To: .htaccess not allowing me to submit forms

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:17 PM

bold: from the looks of it you exclude POST requests from rewriting, so your form target URL is not mapped to a PHP script.

the previous rules seem to have a match on line #5 but proceed to line #10 where you donít end up on a script.
Was This Post Helpful? 0
  • +
  • -

#3 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:25 PM

What does all of this mean? I added in the last part that I put in bold after I realized my POST's/form submissions weren't processing.

This post has been edited by Dormilich: 02 May 2015 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:34 PM

Quote

What does all of this mean?

putting the rules in words?

Quote

I added in the last part that I put in bold after I realized my POST's/form submissions weren't processing.

I take that as proof that my guess is mostly right.
Was This Post Helpful? 0
  • +
  • -

#5 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:41 PM

So how can I "end up on a script"? If that is partially/mostly the solution, it seems from what you are saying. I'm completely unaware of how to write that code. Is it simply moving around what I have already?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:43 PM

Quote

So how can I "end up on a script"?

you see line #5?
Was This Post Helpful? 0
  • +
  • -

#7 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:47 PM

So are you saying that in line 5, the ending php means that when using POST it is searching for a a php file?

Does that mean the php needs taken out?

This post has been edited by Dormilich: 02 May 2015 - 11:49 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: .htaccess not allowing me to submit forms

Posted 02 May 2015 - 11:55 PM

the POST is not searching for anything. the Apache web server does.

itís taking the rules one after another and compares it to the data from the request. it does so for each rule until 1) there are no more rules to process, 2) a rule sefiles a break point (in case of a match).

so for HTTP 1.0 POST /contact it would go (simply my assumption how it works)

line
4 no match
5 match
6 no match
7 no match
8 no match
9 no match
10 match => last rule

hence it tries a HTTP 301 for /contact/
Was This Post Helpful? 0
  • +
  • -

#9 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 03 May 2015 - 12:06 AM

So it is breaking off at that point. If I added the other part of the code under the previous last rule, would that allow it to continue?



RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^er$ er.png [L]
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>


This post has been edited by Dormilich: 03 May 2015 - 12:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: .htaccess not allowing me to submit forms

Posted 03 May 2015 - 12:08 AM

Quote

If I added the other part of the code under the previous last rule, would that allow it to continue?

last rule is last rule. anything after that is ignored.
Was This Post Helpful? 0
  • +
  • -

#11 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 03 May 2015 - 12:15 AM

View PostDormilich, on 03 May 2015 - 12:08 AM, said:

Quote

If I added the other part of the code under the previous last rule, would that allow it to continue?

last rule is last rule. anything after that is ignored.



Is this acceptable to do then?

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$

RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

RewriteRule (.*)$ /$1/ [R=301,L]
RewriteRule ^er$ er.png [L]



Was This Post Helpful? 0
  • +
  • -

#12 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6875
  • View blog
  • Posts: 31,665
  • Joined: 10-May 07

Re: .htaccess not allowing me to submit forms

Posted 03 May 2015 - 03:48 AM

htaccess rules are not php code, moving this topic to Web Servers & Hosting.
Was This Post Helpful? 0
  • +
  • -

#13 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 03 May 2015 - 05:25 PM

Anyone know how I can make this work so my posts will work again? I've tried moving things around with the code I have and nothing is working. I can't find any other information about this other than the initial article I found that I added to my file (The bold part in my code).
Was This Post Helpful? 0
  • +
  • -

#14 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: .htaccess not allowing me to submit forms

Posted 04 May 2015 - 09:26 PM

Someone was trying to help me out and created this file for me and thought it would work, but my site won't even load with it? Anyone know why or if this is the best solution?


RewriteEngine On

# redirect to https
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.mysite/$1 [R,L]

# allow cross origin fonts
<FilesMatch ".(eot|ttf|otf|woff)">
    Header set Access-Control-Allow-Origin "*"
</FilesMatch>

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

# allow php files to respond without extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# remove php extension if provided
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\\.php [L]

Was This Post Helpful? 0
  • +
  • -

#15 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6875
  • View blog
  • Posts: 31,665
  • Joined: 10-May 07

Re: .htaccess not allowing me to submit forms

Posted 04 May 2015 - 11:36 PM

View Postpfar54, on 05 May 2015 - 12:26 AM, said:

Someone was trying to help me out and created this file for me and thought it would work, but my site won't even load with it? Anyone know why

Best way to check why, is to watch the error logs for the vhost.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1