在子目录htaccess的安装Laravel 5应用程序子目录、应用程序、htaccess、Laravel

2023-09-02 09:54:10 作者:甜兔

的设置

我想我的服务器上安装此目录中laravel 5应用程序:

的public_html / MyApp的/

和我希望它在这个URL来访问:

http://www.example.com/myapp/

我创造了这个的.htaccess规则并把它放在了的myapp 文件夹,以便将请求重定向:

  RewriteEngine叙述上
重写规则^(。*)$的公共/ $ 1 [L]
 

问题:

开源应用中心 五分钟教你搭建一个基于Laravel开发博客的应用

在我的浏览器,当我尝试访问该应用程序我重定向的URL一样:

http://www.example.com/myapp/public/index.php http://www.example.com/myapp/public/index.php/dashboard

,而不是像漂亮的pretty的网址:

http://www.example.com/myapp/ http://www.example.com/myapp/dashboard

什么我需要在我的.htaccess文件更改为获得工作正确的网址?

更新:laravel应用程序可以在其他地方被移动,如果需要,只要所需的URL结构上述(含有example.com/myapp/访问的应用程序)就可以实现。

更新:当我尝试访问 example.com/myapp/dashboard 我收到在浏览器中出现以下错误消息:编译 NotFoundHttpException。 PHP的行7793:与开头堆栈跟踪:

 在compiled.php行7793
在RouteCollection->匹配(对象(请求))的compiled.php行7066
在路由器 - > findRoute(对象(请求))的compiled.php行7038
在路由器 - > dispatchToRoute(对象(请求))的compiled.php行7030
在路由器 - >讯(对象(请求))的compiled.php行1989
在内核级>照亮基金会 HTTP  {关闭}(对象(请求))
 

更新:下面的.htaccess文件位于公共文件夹:

 < IfModule mod_rewrite.c>
    < IfModule mod_negotiation.c>
        选项​​-MultiViews
    < / IfModule>

    RewriteEngine叙述上

    #重定向结尾的斜杠...
    重写规则^(。*)/ $ / $ 1 [L,R = 301]

    #拉手前端控制器...
    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则^的index.php [L]

< / IfModule>
 

解决方案

您可以轻松地在的myapp增加额外的的.htaccess 文件实现这一目标文件夹(这将重定向的myapp 所有请求的文件夹复制到公开,因为它应):

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    重写规则^(。*)$的公共/ $ 1 [L]
< / IfModule>
 

您也应该修改的.htaccess 的myapp /公共目录,如:

 < IfModule mod_rewrite.c>
    选项​​-MultiViews
    RewriteEngine叙述上
    的RewriteBase / MyApp的/

    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则。的index.php [L]
< / IfModule>
 

这里的变化是,你重写了基本路径的RewriteBase / MyApp的/

The Setup

I am trying to install a laravel 5 app in this directory on my server:

public_html/myapp/

And I want it to be accessed at this URL:

http://www.example.com/myapp/

I have created this .htaccess rule and placed it in the myapp folder in order to redirect requests :

RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]

Problem:

In my browser when I try to access the app I get redirected URLs like:

http://www.example.com/myapp/public/index.php http://www.example.com/myapp/public/index.php/dashboard

instead of nice pretty URLs like:

http://www.example.com/myapp/ http://www.example.com/myapp/dashboard

What do I need to change in my .htaccess file to get proper URLs working?

Update: The laravel app can be moved elsewhere if needed, as long as the desired URL structure above (with the app accessible at example.com/myapp/) can be achieved.

Update: When I try to access example.com/myapp/dashboard I receive the following error message in the browser: NotFoundHttpException in compiled.php line 7793: with a stack trace that starts with:

in compiled.php line 7793
at RouteCollection->match(object(Request)) in compiled.php line 7066
at Router->findRoute(object(Request)) in compiled.php line 7038
at Router->dispatchToRoute(object(Request)) in compiled.php line 7030
at Router->dispatch(object(Request)) in compiled.php line 1989
at Kernel->IlluminateFoundationHttp{closure}(object(Request))

Update: The following .htaccess file is located in the public folder:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

解决方案

You can easily achieve this by adding additional .htaccess file in the myapp folder (this will redirect all of your requests in myapp folder to the public as it should):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

You should also modify the .htaccess in your myapp/public directory, like this:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On    
    RewriteBase /myapp/

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
</IfModule>

The change here is that you rewrite the base path RewriteBase /myapp/