php .htaccess - 将子域重定向到文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19663849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess - Redirect subdomain to folder
提问by Isaiah
This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
这个问题可能已经被问过一千多次了,但我已经尝试了很多脚本,搜索了很长时间却一无所获,我想,让我们问问吧。
I simply want m.daltonempire.nl
to be redirected to daltonempire.nl/m/
without the user seeing the URL change.
我只是想在用户不看到 URL 更改的情况下m.daltonempire.nl
被重定向到daltonempire.nl/m/
。
So if m.daltonempire.nl/hello.php
is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php
.
所以如果m.daltonempire.nl/hello.php
被请求,我希望用户继续看到这个 URL,而给出的页面实际上是daltonempire.nl/m/hello.php
.
Note: I do not want www.
, so simply http://m.daltonempire.nl
注意:我不想要www.
,所以简单http://m.daltonempire.nl
Thanks in advance,
提前致谢,
Isaiah v. Hunen
以赛亚诉胡嫩
采纳答案by Chetabahana
I have set CNAME of my sub domain below:
我在下面设置了我的子域的 CNAME:
blog.mydomain.com
blog.mydomain.com
to my wordpress that installed in folder /blog/
under root directory.
到我安装在/blog/
根目录下文件夹中的wordpress 。
Formerly I need to use this url to call wordpress:
以前我需要使用这个 url 来调用 wordpress:
http://blog.mydomain.com/blog/
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
这是丑陋的。我尝试了很多代码来重定向:
http://blog.mydomain.com/
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
到文件夹,以便我可以将其用作我的 wordpress 网址。
Finally I got .htaccess
setting that is work:
最后我得到了有效的.htaccess
设置:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/
I have also CNAME other subdomain: http://forum.mydomain.com
to mybb installation in folder /forum/mybb/
so the .htaccess
need to put [L]
on each of RewriteRule code as below.
我也有 CNAME 其他子域:http://forum.mydomain.com
到 mybb 安装在文件夹中,/forum/mybb/
因此.htaccess
需要将[L]
每个 RewriteRule 代码放在下面。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/ [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/ [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.
如果您想使用代码,请不要忘记在应用程序配置文件中设置站点 url 和 cookie 路径,按照设置使重定向正常工作。
回答by Latheesan
Try this example:
试试这个例子:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com
will be mapped to http://example.com/test/...
任何请求http://test.example.com
都将映射到http://example.com/test/...
Try googling dynamic subdomain with php and htaccess
to get better search results.
尝试谷歌搜索dynamic subdomain with php and htaccess
以获得更好的搜索结果。
回答by Ravi Thapliyal
Add this to your .htaccess
in your web root /
directory
将此添加到您.htaccess
的 Web 根/
目录中
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/ [L]
The %{REQUEST_FILENAME}
conditions would let you access /exists.php
and not rewrite it to /m/exists.php
. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
这些%{REQUEST_FILENAME}
条件将允许您访问/exists.php
而不是将其重写为/m/exists.php
. 如果您想重写,请删除这两个,即使这可能会覆盖现有的文件和目录。