我希望能够将所有子域重定向到一个文件夹:
I want to be able to redirect all subdomains to a folder:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\$ RewriteRule (.+)$ "example/subdomains/%1" [L,P]例如,如果某些访问 sub1.example ,它将保留URL,但显示 example/subdomains/sub1 ,并且sub1目录不存在,它将显示 example/404
for example, if some visits sub1.example it will keep the URL but show example/subdomains/sub1 and if the sub1 directory does not exist, it will show example/404
这可能吗?
我尝试了上面的代码,但向我显示了
I tried the above code but its showing me:
Forbidden You don't have permission to access /index.php on this server.Wordpress说:
Wordpress says:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress在我的htaccess文件顶部,是:
and at the top of my htaccess file, is:
RewriteEngine On DirectoryIndex index.php RewriteCond %{HTTP_HOST} admin.domain$ RewriteRule ^(.*)$ /admin/system/$1 [L] 推荐答案当您使用完整的URL作为目标时,.htaccess以上的内容将在外部重定向呼叫.
Your above .htaccess would externally redirect the calls, as you use a full URL as the target.
在您的问题中,您说您想保留主机名,所以我认为这是必需的.
In your question you say you want to keep the hostname, so I will assume that is the requirement.
RewriteEngine On1)将已知子域重写到/subdomains中的目录中
# Rewrite known subdomains to /subdomains/{subdomain}/ RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\$ RewriteCond %{REQUEST_URI} !^/subdomains [NC] RewriteCond %{REQUEST_URI} !^/404 [NC] RewriteRule ^(.+)$ /subdomains/%1/ [L]所以,如果请求是
foo.example/hello浏览器中的URL保持不变,但在内部被映射到
the URL in the browser would stay the same, but internally be mapped to
/subdomains/foo/2)将未知子域重写为/404
# Rewrite missing unknown subdomains to /404/ RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\$ RewriteCond %{REQUEST_URI} ^/subdomains [NC] RewriteCond %{REQUEST_URI} !^/404 [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ /404/ [L]所以,如果请求是
bar.example/hello浏览器中的URL保持不变,但在内部被映射到
the URL in the browser would stay the same, but internally be mapped to
/subdomains/bar/由1)的第一个 RewriteRule
如果/subdomains/bar/不存在,则2)中的第二个 RewriteRule 将插入并在内部映射到
If /subdomains/bar/ does not exist, the second RewriteRule from 2) will kick in and internally map it to
/404/3)测试环境
我实际上使用示例代码对所有这些进行了测试,可在此处找到: github/janpapenbrock/stackoverflow-36497197
更多推荐
htaccess将所有子域重定向到同一目录
发布评论