如何为 Laravel 发布和自定义 .htaccess?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42503379/
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
How to issue and customize .htaccess for Laravel?
提问by mySun
I have just launched a Laravel 5.3 project on a live server and all is going well except one.
我刚刚在实时服务器上启动了一个 Laravel 5.3 项目,除了一个之外,一切都进行得很顺利。
But my issue is, My websites are running on all 3 different URL.
但我的问题是,我的网站在所有 3 个不同的 URL 上运行。
My server is Ubuntu 16.4.
我的服务器是Ubuntu 16.4。
website.com
website.com/public/index.php
website.com/ any word /index.php
网站.com
website.com/public/index.php
website.com/任何词/index.php
My .htaccess
is: (This file is in the root folder)
我的.htaccess
是:(这个文件在根目录下)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
RewriteRule ^(.*)$ public/index.php [L]
</IfModule>
But I just want to see my website with website.com
URL only.
但我只想用website.com
URL来查看我的网站。
回答by M Arfan
Seems like your Laravel app is accesible via an Apache HTTP alias. Follow these steps
似乎您的 Laravel 应用程序可以通过 Apache HTTP 别名访问。按着这些次序
Try to navigate to your expected route prepend it with /index.php/
, in your case: website.com/index.php/users
. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps.
尝试导航到您的预期路线/index.php/
,在您的情况下:website.com/index.php/users
。如果它有效(没有 404),那么您的问题在于 Apache HTTP 的重写模块配置,您应该按照以下步骤操作。
Edit the file public/.htaccess.
编辑文件 public/.htaccess。
Under the line RewriteEngine On
add RewriteBase /
if files are on root directory if in folder like laravel
then add RewriteBase /laravel/
如果文件在根目录中,如果在文件夹中,则在行下RewriteEngine On
添加RewriteBase /
,laravel
然后添加RewriteBase /laravel/
Try to navigate to an existing route.
尝试导航到现有路线。
i.e
IE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
RewriteRule ^(.*)$ public/index.php [L]
</IfModule>
Remove /public from your Laravelcreate a virtual host
从 Laravel 中删除 /public创建一个虚拟主机
go to /etc/apache2/sites-available
create .conf file or update 000-default.conf
去 /etc/apache2/sites-available
创建 .conf 文件或更新 000-default.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/html/index.html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =website.com [OR]
RewriteCond %{SERVER_NAME} =www.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
回答by Siddhesh
This code worked for me. It will remove extention from your website e.g .php and all. Try it once.
这段代码对我有用。它将从您的网站中删除扩展名,例如 .php 和所有内容。尝试一次。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ .php [NC,L]
回答by Chris Townsend
1. I understand why you are trying to change the .htaccess but this can be solved alot simpler. Revert the .htaccess file back to the Laravel standard
1. 我理解您为什么要尝试更改 .htaccess ,但这可以更简单地解决。将 .htaccess 文件恢复到 Laravel 标准
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
2. Move all of your folders and files apart from public and make sure they are behind your HTML folder.
2. 将所有文件夹和文件从 public 中移开,并确保它们位于 HTML 文件夹后面。
3. Move all of your public folders and files out of the public folder and into your html folder
3. 将所有公用文件夹和文件移出公用文件夹并移入 html 文件夹
4. Tell laravel to look in your html folder instead of public by altering the server.php file
4. 通过改变 server.php 文件告诉 laravel 查看你的 html 文件夹而不是 public
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <[email protected]>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/html'.$uri)) {
return false;
}
require_once __DIR__.'/html/index.php';
I find this solution a lot simpler than messing around with you htaccess. You also ensure all of the Laravel core files cannot be accessed from a url.
我发现这个解决方案比与 htaccess 混在一起要简单得多。您还确保无法从 url 访问所有 Laravel 核心文件。
Other issue you are having (Internal pages)
您遇到的其他问题(内部页面)
This is probably due to your apache not allowing override with Laravels htaccess
这可能是由于您的 apache 不允许使用 Laravel htaccess 进行覆盖
Go to the command line of your server and access the apache2.conf file
转到服务器的命令行并访问 apache2.conf 文件
cd /etc/apache2
Open the apache2.conf file with nano
用nano打开apache2.conf文件
sudo nano apache2.conf
Now locate the following
现在找到以下
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change this to
将此更改为
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Writeout the changes and exit nano.
写出更改并退出 nano。
Restart apache
重启apache
sudo service apache2 restart
It should now be working :)
它现在应该可以工作了:)
回答by lax1089
To only match the homepage, i.e. http://domainxyz.com/
you need to match the empty path (as mod_Rewrite removes the leading /
.
只匹配主页,即http://domainxyz.com/
你需要匹配空路径(因为 mod_Rewrite 删除了前导/
.
RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC]
RewriteRule ^$ /view.php?page=process/ [NC,L]
回答by Joe
Try using this in your .htaccess
file:
尝试在您的.htaccess
文件中使用它:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com/([a-zA-Z-]*)/index.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com/([a-zA-Z-]*)/index.php [NC]
RewriteRule ^(.*)$ http://www.website.com [L,R=302,NC]
Make sure you clear your cache before testing this. You will notice I've used R=302
, this is a temporary redirect. If the redirect works and you're happy with it, then switch that to R=301
which is a permanent redirect.
确保在测试之前清除缓存。您会注意到我使用了R=302
,这是一个临时重定向。如果重定向有效并且您对此感到满意,则将其切换R=301
为永久重定向。