You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9部署HostGator虚拟主机后表单提交MethodNotAllowedHttpException错误解决

解决Laravel 9部署到HostGator后表单提交的MethodNotAllowedHttpException错误

我用Laravel 9开发的项目在本地运行正常,但部署到HostGator虚拟主机后,点击联系表单提交时出现以下错误:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST.

我的blade表单代码

<div class="content">
@include('partials.alerts')
    <h1 class="heading">Contact Us</h1>
    
    
    <form method="post" action="{{url('form')}}#contact">
        {{method_field('POST')}} 
            {{csrf_field()}}
            <div class="form">
  <div class="input-flex">
    <input type="text" name="name" placeholder="Name*" /><br><br>
    <span style="color:red">@error('name'){{$message}}@enderror</span>
    <input type="email" name="email" placeholder="E-mail*" /><br><br>
    <span style="color:red">@error('email'){{$message}}@enderror</span>
    <input type="tel" class="full-width" name="telephone" placeholder="Phone number*" /><br><br>
    <span style="color:red">@error('telephone'){{$message}}@enderror</span>
    <textarea cols="2" rows="2" class="full-width" name="message" placeholder="Message"></textarea>
   </div>
  <button  class="conbtn" type="submit">Submit</button>
</form>

web.php路由配置

Route::get('/', function () {
    return view('welcome');
});

Route::post('form','App\Http\Controllers\UserController@store');

Route::get('/user/verify/{token}', 'App\Http\Controllers\UserController@verifyUser');

问题原因及解决办法

1. 移除表单action中的锚点

问题核心是表单action里的#contact锚点,HostGator服务器可能会将带锚点的POST请求误解析为GET请求,导致路由匹配失败。

修改表单action,去掉锚点:

<form method="post" action="{{ url('form') }}">

如果提交后需要回到带锚点的页面,在UserController的store方法中添加重定向逻辑:

// 先给首页路由命名
// web.php中修改:Route::get('/', function () { return view('welcome'); })->name('welcome');

return redirect()->route('welcome')
    ->with('success', '表单提交成功')
    ->fragment('contact');

2. 更新路由缓存

部署到主机后,执行路由缓存命令确保路由规则生效:

php artisan route:cache

若主机不支持终端操作,可联系服务商帮忙执行,或临时添加路由清除缓存(执行后删除即可):

Route::get('/clear-cache', function() {
    Artisan::call('route:cache');
    return "路由缓存已更新";
});

3. 清理冗余的表单方法声明

表单已指定method="post",无需额外添加{{method_field('POST')}},直接移除该代码即可,避免不必要的请求方法冲突。

4. 检查.htaccess配置

确保主机根目录下的.htaccess文件包含Laravel默认的URL重写规则,否则会导致路由无法正确匹配:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

内容的提问来源于stack exchange,提问作者Narakaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 15:40:11