Php .htaccess文件-通配符子域重定向

Php .htaccess文件-通配符子域重定向,php,apache,.htaccess,mod-rewrite,redirect,Php,Apache,.htaccess,Mod Rewrite,Redirect,我有很多子域配置使用通配符,例如 subdomain1.domain.com subdomain2.domain.com subdomain3.domain.com (...) subdomain89.domain.com 等等 它们指向/public\u html/。在我创建的公共html中 /public_html/subdomain1 /public_html/subdomain2 /public_html/subdomain3 (..) /public_html/subdomain89

我有很多子域配置使用通配符,例如

subdomain1.domain.com
subdomain2.domain.com
subdomain3.domain.com
(...)
subdomain89.domain.com
等等

它们指向/public\u html/。在我创建的公共html中

/public_html/subdomain1
/public_html/subdomain2
/public_html/subdomain3
(..)
/public_html/subdomain89
子文件夹

我想将来自子域的所有请求重定向到各个子文件夹中的index.php文件,例如:

http://subdomain1.domain.com/
http://subdomain1.domain.com/about_us.php
http://subdomain1.domain.com/contact.php
重定向到/public_html/subdomain1/index.php

http://subdomain2.domain.com/
http://subdomain2.domain.com/about_us.php
http://subdomain2.domain.com/contact.php
重定向到/public_html/subdomain2/index.php等

这是我的.htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/index.php [PT,L]
当我访问subdomain1.domain.com时,我看到了/public_html/subdomain1中的index.php文件,但当我访问subdomain1.domain.com/about_us.php时,我得到了404。有什么想法吗


谢谢,我已经弄明白了。这是工作代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

:-

首先确保.htaccess文件位于文档根目录中与index.php相同的位置,否则它只会递归地影响它所在的子文件夹以及其中的任何子文件夹

接下来,对规则做一个细微的更改,使其看起来像:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
目前,你正在进行匹配。哪一个是任何字符的一个实例,您至少需要。*才能匹配任何字符的任意数量的实例

$_GET['path']变量将包含伪目录结构,例如/mvc/module/test,您可以在index.php中使用它来确定要执行的控制器和操作

如果您希望将整个shebang安装在一个子目录中,例如/mvc/或/framework/中,最简单的方法是稍微更改重写规则,以将其考虑在内

RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]
并确保index.php位于该文件夹中,而.htaccess文件位于文档根目录中

2月18日和1月19日更新的$_GET['path']替代方案

将path设置为$\u GET变量实际上是不必要的,甚至现在也不常见,许多框架将依赖$\u SERVER['REQUEST\u URI']来检索相同的信息-通常用于确定使用哪个控制器-但原理完全相同

这确实稍微简化了RewriteRule,因为您不需要创建path参数,这意味着OP的原始RewriteRule现在可以工作了:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
但是,关于在子目录中安装的规则仍然适用,例如

RewriteRule ^.*$ /mvc/index.php [L,QSA]
也许这可以帮助你:为什么你有PT标志?