404重新加载NotFoundHttpException - Laravel + AngularJS

时间:2015-12-14 15:50:24

标签: angularjs html5 .htaccess laravel-5.1

所以我用Google搜索了如何在使用带有laravel的angularjs时从url中删除#标签。主要是所有资源如下:

  1. $locationProvider.html5Mode(true);添加到a​​pp.js
  2. 在布局页面base tag更新index.php。我的如下:<base href="/myproject/public/">
  3. 它工作顺利,没有任何问题,直到我在工作时尝试刷新页面,所以我得到了以下内容:

    NotFoundHttpException in RouteCollection.php line 161:
    

    我检查了现有的solutions,它主要声明在根目录中创建.htaccess,添加以下内容:

    RewriteEngine On  
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    
    # If the requested resource doesn't exist, use index.html
    RewriteRule ^ /index.html
    

    但它仍然只在第一次工作,每当我重新加载时,没有任何作用。

    另一个answer声明要将以下内容添加到路由文件中,但是当我尝试它时,我得到了以下内容:

    FatalErrorException in Facade.php line 216:
    Call to undefined method Illuminate\Foundation\Application::missing()
    

    更新

    我更新了.htaccess文件,如下所示:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
        <IfModule mod_proxy_http.c>
            RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
            RewriteCond %{QUERY_STRING} _escaped_fragment_
    
            # Only proxy the request to Prerender if it's a request for HTML
            RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent))(index\.php)?(.*) http://service.prerender.io/%{REQUEST_SCHEME}://%{HTTP_HOST}/$3 [P,L]
        </IfModule>
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
        RewriteCond %{HTTP:Authorization} ^(.*)
        RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
    </IfModule>
    

    在服务器上我更新了/etc/apache2/sites-enabled/000-default.conf如下:

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/myproject/public
            <Directory /var/www/myproject/public>
                    AllowOverride All
                    RewriteEngine On
                    RewriteBase /var/www/myproject/public
                    RewriteCond %{REQUEST_FILENAME} -f [OR]
                    RewriteCond %{REQUEST_FILENAME} -d
                    RewriteRule ^ - [L]
                    RewriteRule ^ index.php [L]
            </Directory>
    </VirtualHost>
    

    哈希从网址中删除,但每当我刷新主页以外的页面时,它都会出现404错误。知道什么是缺失的吗?!

1 个答案:

答案 0 :(得分:0)

您需要将网址重写更改为:

<rewrite>
 <rules> 
  <rule name="Main Rule" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll">
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />  
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/" />
  </rule>
</rules>
</rewrite>

查看完整的详细信息 AngularJS HTML5 mode reloading page not found solution