【问题标题】:Laravel .htaccess configuration in folders文件夹中的 Laravel .htaccess 配置
【发布时间】:2018-12-06 22:14:04
【问题描述】:

情况:这是文件夹结构的webproject。

  • 万维网
    • 2015
    • 2016
    • 2017
    • 2018
    • 当前

每个文件夹代表当年的网站,在“当前”文件夹中是您访问 www.mydomain.com 时应该显示的网站。 目标是,如果您访问 www.mydomain.com/2017,您会看到 2017 年的网站。

根文件夹中有一个 .htaccess 文件,可确保如果您导航到“/”,您将被重定向到当前文件。

我正在为必须放入“2017”文件夹(这是一个 laravel 应用程序)中的 .htaccess 文件而苦苦挣扎。

www文件夹中的.htaccess文件

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteEngine on

   RewriteRule ^(.*)$ current/$1 [L]
</IfModule>

2017文件夹中的.htaccess文件

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteBase /2017
   RewriteEngine on
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

当前结果:当我转到 www.mydomain.com/2017 时,我转到 2017 文件夹,但它转到 2017 年的“2017”页面。为了测试这一点,我刚刚在我的 web.php 路由中添加了这个:

Route::get('/{page}', array('as'=>'renderpage',function ($page) {
    dd('I am here and the page you request is '.$page);
}));

导致: "我在这里,您请求的页面是 2017"

长话短说:如何从 .htaccess 文件的 url 中删除“2017”,以便 laravel 将 www.mydomain.com/2017 解释为 2017 文件夹的根目录。最好在 2017 文件夹中的 .htaccess 文件中完成。

编辑: 我将2017文件夹中的.htaccess文件改成这样:

<IfModule mod_rewrite.c>
   Options +SymLinksIfOwnerMatch
   RewriteBase /2017
   RewriteEngine on
   RewriteRule ^(/.*|)$ public/$1 [L,NC,R=301]
</IfModule>

现在它可以工作了,除了您现在看到的网址 www.mydomain.com/2017/public 如何从 url 中删除“public”?

【问题讨论】:

  • 如果我理解你的话,你希望当有人请求www.mydomain.com时,它应该直接进入2017文件夹并进一步执行?
  • @vivek_23 不,我在那种情况下解释错了......如果你去 www.mydomain.com 你应该去“当前”(这有效)。如果你去 www.mydomain.com/2017 你应该去 2017。这也是正在发生的事情,但实际上你去 2017 文件夹内的“2017”页面,它应该去 2017 的“根”页面.
  • @AndriesHeylen 每当谈到 Laravel 路由时,您都无法将请求解释为文件夹。这是一个 URL 路径,与文件夹无关。您的Route::get('/{page}'www.mydomain.com/2017 完美匹配,{page} 在此处变为2017。但是,您可以在 Laravel 控制器或闭包中处理值,即 page 值并正确重定向用户。
  • @AndriesHeylen 另外,为什么2016,2017 等在current 之外?
  • @vivek_23 因为到目前为止我们都是这样做的,每年都会将“当前”文件夹重命名为去年,并且在当前文件夹中安装一个新网站。

标签: php laravel apache .htaccess


【解决方案1】:

也许这是在 laravel 中的正确做法:

您可以使用更改控制器或路由中的根目录

$this->app->bind('path.public', function() {
  return base_path().'/public_html';
});

我猜你可以在里面插入一个绝对路径。

退房

https://laravel.com/docs/5.7/helpers#method-public-path

https://laravel.com/docs/5.7/helpers#method-base-path

【讨论】:

  • 不触碰“laravel”代码可以做到吗?我更喜欢那个
  • 我以为您正在寻找 laravel 解决方案。 Options +SymLinksIfOwnerMatch RewriteBase / RewriteEngine on RewriteRule ^(.*)$ 2017/public/$1 [L] 或者你可以为 index2017.php 创建一个链接到子文件夹的符号链接?
【解决方案2】:

无需接触 laravel 代码,而是使用 nginx 配置:

server {
    listen *:8080;
    server_name 2016.test.test;

    root /var/www/html/2016;
    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_index   index.php;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
} 

 server {
    listen                *:80;

    server_name           www.test.test;
    client_max_body_size 1m;

    root /var/www/html/current;
    index  index.html index.htm;

    location /2016/ {
        rewrite /2016/(.*) /$1 break;

        proxy_pass          http://2016.test.test:8080;
        proxy_redirect      off;
        proxy_set_header    Host $host;
    }
}

文件夹结构:

.
├── 2015            
│   └── index.html  
├── 2016            
│   ├── index.html  
│   └── index.php   
├── 2017            
│   └── index.html  
├── current         
│   ├── index.html  
│   └── test.html   
└── index.html

index.php:

 <?php
     var_dump($_SERVER);
 ?> 

结果调用: http://www.test.test/2016/sample/foo/bar ...

  ...
  'SCRIPT_FILENAME' => string '/var/www/html/2016/index.php' (length=28)
  'REDIRECT_STATUS' => string '200' (length=3)
  'SERVER_NAME' => string '2016.test.test' (length=14)
  'SERVER_PORT' => string '8080' (length=4)
  'SERVER_ADDR' => string '127.0.0.1' (length=9)
  'REMOTE_PORT' => string '48598' (length=5)
  'REMOTE_ADDR' => string '127.0.0.1' (length=9)
  'SERVER_SOFTWARE' => string 'nginx/1.14.0' (length=12)
  'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
  'REQUEST_SCHEME' => string 'http' (length=4)
  'SERVER_PROTOCOL' => string 'HTTP/1.0' (length=8)
  'DOCUMENT_ROOT' => string '/var/www/html/2016' (length=18)
  'DOCUMENT_URI' => string '/index.php' (length=10)
  'REQUEST_URI' => string '/sample/foo/bar' (length=15)
  'SCRIPT_NAME' => string '/index.php' (length=10)
  ...

你看到 REQUEST_URI 只是 '/sample/foo/bar' 没有你的年份

这个例子被硬编码到 2016 年,您可以轻松地使用一些正则表达式来表示年份的位置,例如:20[0-9]{2} 并用适当的变量($1 或类似的东西)替换匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-31
    • 2019-10-16
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多