2017-02-28 59 views
7

我剛剛在一臺實時服務器上啓動了Laravel 5.3項目,除了一臺以外,所有項目都進展順利。如何爲Laravel發佈和定製.htaccess?

但我的問題是,我的網站在所有3個不同的URL上運行。

我的服務器是Ubuntu 16.4。

  1. website.com

  2. website.com/public/index.php

  3. website.com/任何單詞的index.php

.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 ^(.*)/$ /$1 [L,R=301] 

    RewriteRule ^(.*)$ public/index.php$1 [L] 
</IfModule> 

但我只想看到我的網站只有website.com的URL。

+0

是喜是要刪除/公共形成你的網站...? –

+0

嗨,糾正我,如果我錯了。您希望您的所有網址都來自example.com,而不是www.example.com。除了那些以/index.php結尾的url之外的所有url將被重定向到example.com? – TheChetan

回答

1

要僅http://domainxyz.com/需要匹配空路徑(如mod_rewrite的刪除前導/匹配的網頁,即

RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC] 
RewriteRule ^$ /view.php?page=process/ [NC,L] 
+0

嗨,我不使用例如'/view.php?page=process/'我的框架,我的問題是看到'website.com'這個'website.com/任何word/index.php'網址。 – mySun

1

嘗試在你的.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] 

製作確定你在測試之前先清空你的緩存,你會注意到我已經使用了R=302,這是一個臨時重定向,如果重定向工作,並且你對它滿意,那麼把它切換到R=301這是一個永久的重定向。

+0

嗨,謝謝你的幫助,但不要工作這段代碼:-( – mySun

+0

我的文件是這樣的路線:'/ var/www/html'。 – mySun

2

似乎您的Laravel應用程序可通過Apache HTTP別名訪問。請按照這些步驟

嘗試導航到您的預期路線前加上/index.php/,在您的情況:website.com/index.php/users。如果它工作(不404),那麼你的問題是Apache HTTP的重寫模塊配置,你應該按照下面的步驟。

編輯文件public/.htaccess。

在訂單RewriteEngine On添加RewriteBase /如果文件是在根目錄下,如果在像laravel文件夾,然後添加RewriteBase /laravel/

嘗試導航到一個現有的航線。

<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 ^(.*)/$ /$1 [L,R=301] 

    RewriteRule ^(.*)$ public/index.php$1 [L] 
</IfModule> 

卸下/公衆從Laravel 創建一個虛擬主機

/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> 
+0

嗨,謝謝你的幫助,我在'RewriteBase /'中加入'.htaccess'文件在根文件夾和公共文件夾中。只有這樣的情況下,true:'website.com/ any word/index.php',現在我可以用'website.com/public/index.php'打開網站:-( – mySun

+0

@mySun現在工作正常..你仍然有問題..? –

+0

我的示例代碼是在根項目的'.htaccess'文件中。現在在'RewriteEngine On'行下面添加'RewriteBase /'文件' public/.htaccess.',但我可以通過網站看到我的網站。com/public/index.php'和'website.com/public'。 – mySun

1

此代碼爲我工作。它會消除您網站的延伸,例如。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 ^(.*?)/?$ $1.php [NC,L] 
+0

嗨,我已經添加此代碼到'公共/ .htaccess'或'.htaccess'在根? – mySun

+0

您可以在兩個地方添加此項。但公共/ .htaccess是我的建議。 – Siddhesh

+0

我有追加這個代碼或覆蓋? – mySun

1

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 ^(.*)/$ /$1 [L,R=301] 

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

2.將您的所有文件夾和文件的除了公衆,並確保他們的背後你的HTML文件夾中。

3.把你所有的公共文件夾和文件進行公用文件夾的和你的HTML文件夾

4.泰爾laravel通過改變服務器查找在HTML文件夾,而不是公共的。 PHP文件

<?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'; 

我覺得這個解決方案不是與你的htaccess亂搞簡單得多。你也可以確保所有的Laravel核心文件都不能通過url訪問。

其他問題你有(內部頁)

這可能是由於你的Apache不允許覆蓋與Laravels htaccess的

轉到您的服務器的命令行和訪問apache2.conf文件

cd /etc/apache2 

納米打開apache2.conf文件

sudo nano apache2.conf 

現在找到以下

<Directory /var/www/> 
     Options Indexes FollowSymLinks 
     AllowOverride None 
     Require all granted 
</Directory> 

更改爲

<Directory /var/www/> 
     Options Indexes FollowSymLinks 
     AllowOverride All 
     Require all granted 
</Directory> 

WriteOut的更改並退出nano。

重新啓動Apache

sudo service apache2 restart 

現在應該工作:)