php Apache Mod 重写 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12448912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:36:30  来源:igfitidea点击:

Apache Mod Rewrite For Laravel

phpapachelaravel

提问by Ody

I have an installation of Laravel on Wampserver. The directory is as follows:

我在 Wampserver 上安装了 Laravel。目录如下:

C:\wamp\www\laravel

C:\wamp\www\laravel

Now URLs are like this:

现在网址是这样的:

http://localhost/laravel/public/index.php/home/index

http://localhost/laravel/public/index.php/home/index

So I used the following htaccess code

所以我使用了以下 htaccess 代码

Options +FollowSymLinks
Options -indexes
DirectoryIndex index.PHP
RewriteEngine on
RewriteCond  !^(index\.PHP|images|robots.txt)
RewriteCond %{REQUEST_ FILENAME} !-f
RewriteCond %{REQUEST_ FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L, QSA]

To reduce the URL to

将 URL 减少到

http://localhost/laravel/public/home/index

http://localhost/laravel/public/home/index

But the laravel framework insists that all application files reside in the public folder.

但是 Laravel 框架坚持所有应用程序文件都驻留在公共文件夹中。

So I would like to know what I need to add to (or subtract from) the htaccess file so that the URL can look like

所以我想知道我需要在 htaccess 文件中添加(或减去)什么,以便 URL 看起来像

http://localhost/laravel/home/index

http://localhost/laravel/home/index

Thanks

谢谢

回答by Jason Lewis

When testing locally I do one of two things.

在本地测试时,我会做两件事之一。

  1. Create a new .htaccess below the public directory with the following.

    <IfModule mod_rewrite.c>
        RewriteEngine on
    
        RewriteRule ^(.*)$ public/ [L]
    </IfModule>
    
  2. Create a new virtual host. With WAMP you can navigate to C:\wamp\bin\apache\YOUR APACHE VERSION\conf\extraand find your httpd-vhosts.conffile, in there you can see example virtual hosts. Here's one of mine:

    <VirtualHost *:80>
        DocumentRoot "c:/wamp/www/laravel/public"
        ServerName laravel.dev
        ServerAlias www.laravel.dev
    </VirtualHost>
    

    Make sure that your vhosts configuration file is being included. Open up your httpd.conffile and search for the vhostsfile, uncomment the include line if it's commented out. Then I open the CLI and enter notepad "C:\windows\system32\drivers\etc\hosts"which opens up your hosts file. Underneath the item that mentions localhostplace your new host. Here's an example.

    127.0.0.1  laravel.dev
    

    Make sure you restart Apache and bingo, you should be able to navigate to http://laravel.devand you won't have any annoying public directory. This is how I achieve it, as I prefer the nicer looking virtual host rather then a long winded localhostURL.

  1. 使用以下内容在公共目录下创建一个新的 .htaccess。

    <IfModule mod_rewrite.c>
        RewriteEngine on
    
        RewriteRule ^(.*)$ public/ [L]
    </IfModule>
    
  2. 创建一个新的虚拟主机。使用 WAMP,您可以导航C:\wamp\bin\apache\YOUR APACHE VERSION\conf\extra并找到您的httpd-vhosts.conf文件,在那里您可以看到示例虚拟主机。这是我的一个:

    <VirtualHost *:80>
        DocumentRoot "c:/wamp/www/laravel/public"
        ServerName laravel.dev
        ServerAlias www.laravel.dev
    </VirtualHost>
    

    确保包含您的 vhosts 配置文件。打开您的httpd.conf文件并搜索该vhosts文件,如果已注释掉,则取消注释包含行。然后我打开 CLI 并输入notepad "C:\windows\system32\drivers\etc\hosts"它打开您的主机文件。在提及的项目下方localhost放置您的新主机。这是一个例子。

    127.0.0.1  laravel.dev
    

    确保重新启动 Apache 和宾果游戏,您应该能够导航到http://laravel.dev并且不会有任何烦人的公共目录。这就是我实现它的方式,因为我更喜欢看起来更漂亮的虚拟主机,而不是冗长的localhostURL。

Hope this helps.

希望这可以帮助。

回答by Ody

I finally figured a way out. First of all, I had to open and edit my Apache httpd.conf by selecting it from the Wamp Aestran tray menu. The I had to uncomment the line

我终于找到了出路。首先,我必须通过从 Wamp Aestran 托盘菜单中选择它来打开和编辑我的 Apache httpd.conf。我必须取消注释该行

#Include conf/extra/httpd-vhosts.conf

#Include conf/extra/httpd-vhosts.conf

After that, I opened the file which is located at the

之后,我打开了位于

<wampdirectory>/bin/apache/apache.x.y.z/conf/extra/httpd-vhosts.conf

<wampdirectory>/bin/apache/apache.x.y.z/conf/extra/httpd-vhosts.conf

then I added the following lines.

然后我添加了以下几行。

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80


<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName localhost
    Options Indexes FollowSymLinks
    <Directory "C:/wamp/www">
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Allow from 192.168.0.100
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Allow from 192.168.0
    </Directory>
</VirtualHost>

## must be first so the the wamp menu page loads when you use just localhost as the domain name

<VirtualHost *:80>
    DocumentRoot "C:/wamp/sites/laravel/public"
    ServerName laravel.dev
    Options Indexes FollowSymLinks
    <Directory "C:/wamp/sites/laravel/public">
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Allow from 192.168.0.100
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Allow from 192.168.0
    </Directory>
</VirtualHost>

The next step was to edit my hosts file at C:\windows\system32\drivers\etc

下一步是在 C:\windows\system32\drivers\etc 编辑我的主机文件

and added

并添加

127.0.0.1 laravel.dev

127.0.0.1 laravel.dev

Then restarted Wamp and it worked. Thanks to you guys for pointing me in the right direction. Really Appreciate it

然后重新启动 Wamp 并且它起作用了。感谢你们为我指明了正确的方向。真的很欣赏

回答by Relaxing In Cyprus

The easiest way I got this working on my local dev environment was to do the following:

我在本地开发环境中使用它的最简单方法是执行以下操作:

(Assuming you have WAMP installed in C:\WAMP)

(假设您在 C:\WAMP 中安装了 WAMP)

Create the following folder:

创建以下文件夹:

c:\wamp\www\laravel

Download laravel and put the contents in the above directory. You will know you have done it right if you can browse to hxxp://localhost/laravel/public and get the opening screen. However, this isn't good enough. We want to get that screen by going to http://localhost/laravel

下载laravel,把内容放到上面的目录下。如果您可以浏览到 hxxp://localhost/laravel/public 并获得初始屏幕,您就会知道您做对了。然而,这还不够好。我们想通过去获得那个屏幕http://localhost/laravel

So then we do the following:

那么我们执行以下操作:

Create a textfile containing the following:

创建一个包含以下内容的文本文件:

Alias /laravel "c:/wamp/www/laravel/public" 

<Directory "c:/wamp/www/laravel/public">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Allow from all
</Directory>

Save this file as laravel.confin the c:\wamp\alias directory.

将此文件保存laravel.conf在 c:\wamp\alias 目录中。

Finally, restart your wampserver.

最后,重新启动您的 wampserver。

You should now be able to surf to http://localhost/laravel

你现在应该可以上网了 http://localhost/laravel

Note that the above is strictly for a local development environment.

请注意,以上仅针对本地开发环境。

回答by Jürgen Paul

You're gonna end up with your code and your public folder residing in the same place, which most people do not recommend. I'd suggest you take advantage of using a local web server.

您最终会将代码和公用文件夹放在同一个地方,这是大多数人不推荐的。我建议您利用使用本地网络服务器。

Why not make mysite.devpoint to laravel/publicdirectory so you could just use http://mysite.deveverytime, you have cleaner and shorter URL's too?

为什么不mysite.dev指向laravel/public目录,以便您http://mysite.dev每次都可以使用,您也有更干净和更短的 URL?

回答by Ralph Frost

As a newb to WAMP and Laravel, I struggled at bit but did get the virtualhost thing to work on my WIN7PRO 64-bit box. In WAMPSERVER/Apache/hppd.conf at the end of the file, I added:

作为 WAMP 和 Laravel 的新手,我有点挣扎,但确实让虚拟主机在我的 WIN7PRO 64 位机器上工作。在文件末尾的 WAMPSERVER/Apache/hppd.conf 中,我添加了:

NameVirtualHost *:80

<VirtualHost *:80>
 DocumentRoot C:/webapp/public
 ServerName webapp
 <Directory C:/webapp/public >
   Options Indexes FollowSymLinks MultiViews
   AllowOverride all
   Order Deny,Allow
   Allow from All
 </Directory> 
</VirtualHost> 

<VirtualHost *:80>
 DocumentRoot C:/wamp/www
 ServerName localhost
</VirtualHost> 

and I added:

我补充说:

127.0.0.1 webapp

127.0.0.1 网络应用程序

to the hosts file. (I was never successful editing the vhosts files, as many posts on the web suggested.)

到主机文件。(我从未成功编辑 vhosts 文件,正如网络上的许多帖子所建议的那样。)

These changes allow me to get to my Laravel test app in my browser via

这些更改使我可以通过以下方式在浏览器中访问我的 Laravel 测试应用程序

http://webapp
(and also, via just http://127.0.0.1)

and, to get to all my other sites, via:

并且,要访问我所有的其他网站,请通过:

http://localhost/devsite/whatever..