mod_rewrite is preventing my form to be submitted

2019-08-10 21:59发布

I have created a www.example.com and m.example.com. The m.example.com resides inside the htdocs/m/ folder. I am also deleting the '.php' extensions using .htaccess. I am having the below codes on the root directory:

RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteRule ^(.*)$ m/$1 [L]

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

I have forms in the m.example.com. For example in m.example.com/myform.php, I have

<form method="post" action="processform.php">
 .....
</form>

I want it to be submitted to the m/ folder (/m/processform.php), but now it always submit to the processform.php of the main directory. I have tried action="//m.example.com/processform.php" but no luck.

Anyone know kow to solve it? Thanks!

Update: if the method of the form is get instead of post, the form will be submitted correctly (to /m/processform.php)

2条回答
男人必须洒脱
2楼-- · 2019-08-10 22:46

It's possible the first redirect is interferring with the POST request, try changing the request regex to:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
查看更多
Ridiculous、
3楼-- · 2019-08-10 22:47

Actually your rules are skipping POST requests from all rewrite rules. You can tweak the rules to skip POST from external redirects and keep internal rewrites active/enabled for POST like this:

# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteRule ^((?!m/).*)$ m/$1 [L,NC]

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

PS: I have also simplified your 2nd rule.

查看更多
登录 后发表回答