mod_rewrite is preventing my form to be submitted as desired

笑着哭i 提交于 2020-01-15 12:27:28

问题


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)


回答1:


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.




回答2:


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]


来源:https://stackoverflow.com/questions/26531318/mod-rewrite-is-preventing-my-form-to-be-submitted-as-desired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!