D0nkey
D0nkey

Reputation: 80

.htaccess modrewrite breaks my forms

I've added a modrewrite to my .htaccess and now all of my forms don't send post data to php scripts.

Code: eg: my .htaccess removes the .php extension, removes the trailing slash if it's a directory.

RewriteEngine On

# Redirect index to site root
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]
RewriteRule ^index.php$ http://www.mysite.com/ [R=301,L]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [QSA,R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [QSA,L]

Is there a way of adding exceptions to the modrewrite rules so certain files/directories are ignored? So all files called form-process.php, email-process.php, upload-process.php, save-process.php could be ignored?

edit: I've just been reading that I can add !POST to my query. If I add it like this, would it work?

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ !POST
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f !POST
RewriteRule ^([^/.]+)$ $1.php [L]

edit: also added [QSA] to the rules above, but still no variables are passed to my forms

edit: For anyone else stuck with this problem I fixed it by selecting the files that posted data and removing them from the changes to the .htaccess file. It's not ideal, but it works for now.

# ignore recipes page
RewriteRule ^directorytoremove - [L,NC]
RewriteRule ^filenametoremove - [L,NC]

Redirect 301 does not pass any data via POST, even if you add QSA. So you need to do a post-redirect-get eg your form POSTS the data, is redirected and the receiving file GETS the data and processes it. If you are having the same problem, paste the code above at the top of your .htaccess file below the opening RewriteEngine On statement.

Hope that helps anyone with the same problem!

Upvotes: 3

Views: 480

Answers (2)

Oussama Jilal
Oussama Jilal

Reputation: 7739

You can add a rule like this in the beginning (just after the RewriteEngine on)

RewriteRule ^(form|email|upload|save)-process\.php$ - [L]

Upvotes: 2

CappY
CappY

Reputation: 1580

Try this:

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/.]+)$ $1.php [QSA,L]

Upvotes: 0

Related Questions