Php 将子域指向子目录
我使用Laravel将php页面生成到名为Php 将子域指向子目录,php,.htaccess,redirect,laravel-5,Php,.htaccess,Redirect,Laravel 5,我使用Laravel将php页面生成到名为webpage的子目录中。我想允许用户使用子域访问页面。例如,我在webpage目录中生成了一个文件whoami.php。如果人们试图访问whoami.domain.com,他可以看到whoami.php。我在网页中安装了一个index.php,它可以提取别名并显示文件。我想我需要的是在不重定向的情况下将子域传递到index.php文件。我写的htaccess如下 <IfModule mod_rewrite.c> <IfModu
webpage
的子目录中。我想允许用户使用子域访问页面。例如,我在webpage
目录中生成了一个文件whoami.php
。如果人们试图访问whoami.domain.com
,他可以看到whoami.php
。我在网页
中安装了一个index.php
,它可以提取别名并显示文件。我想我需要的是在不重定向的情况下将子域传递到index.php文件。我写的htaccess如下
<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]
#sub domain custom part
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/webpage/%1/$1 [L,NC,QSA]
</IfModule>
要将子域重定向到子文件夹,您可能需要将以下内容放在“RewriteEngine On”(重写引擎打开)行的后面:
我有一个简单的解决方案。因为我使用的是Laravel,并且已经使用了
index.php
作为引导,所以我在那里编写了一些php代码来实现我的目标。如果需要,你们可以用。如果我的解决方案不够好,请告诉我。谢谢
我的解决方案
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link = str_replace(array('http://', 'https://', 'www.'), '', $link);
$splitted = explode(".", $link);
if($splitted[0] != 'domain'){
$file_name = $splitted[0].'.php';
$file_loc = 'webpage/'.$file_name;
if(file_exists($file_loc)) {
include($file_loc);
} else {
header('Location: http://domain.com/not-found');
}
die();
}
我现在在重定向循环中尝试了你的解决方案。我已经用我所做的代码更新了问题。你能检查一下吗。谢谢
RewriteCond %{HTTP_HOST} ^(sub)\.domain\.com$
RewriteRule ^(.*)$ /webpage/%1/$1 [NC,R,L]
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link = str_replace(array('http://', 'https://', 'www.'), '', $link);
$splitted = explode(".", $link);
if($splitted[0] != 'domain'){
$file_name = $splitted[0].'.php';
$file_loc = 'webpage/'.$file_name;
if(file_exists($file_loc)) {
include($file_loc);
} else {
header('Location: http://domain.com/not-found');
}
die();
}