【发布时间】:2011-06-24 03:25:52
【问题描述】:
想要的重写行为(内部重写!)
http://<subdomain>.domain.tld/<path> -> /<subdomain>/<path>
http://www.domain.tld/path/file.php -> /www/path/file.php
http://project.domain-tld/index.php -> /project/index.php
文件夹结构:
/ root
.htaccess
/www www.domain.tld
index.php
/www
file.php
/foo
/bar
file.php
/project project.domain.tld
index.php
someRandomFiles
/somesubdomain somesubdomain.domain.tld
index.php
someRandomFiles
/anothersubdomain anothersubdomain.domain.tld
index.php
someRandomFiles
完整的.htaccess
# Unicode
AddDefaultCharset utf-8
# Activate mod_rewrite
RewriteEngine on
RewriteBase /
# Subdomains
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.janbuschtoens\.de(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
我的.htaccess 似乎有效。你可以在这里进行现场测试:
-
http://test.janbuschtoens.de/
重写为
/test/ -
http://www.janbuschtoens.de/
重写为
/www/
但是子目录中有一些奇怪的行为。如果请求路径中的第一个目录与子域本身同名,mod_rewrite 似乎会忽略该规则。例如:
http://www.domain.tld/foo/bar/file.php -> /www/foo/bar/file.php - Fine!http://www.domain.tld/ -> /www/ - Fine!http://www.domain.tld/www/ -> /www/ - Should be: /www/www/http://www.domain.tld/www/www/ -> /www/www/ - Should be: /www/www/www/
对于另一个现场测试:
-
http://test.janbuschtoens.de/ 重写为
/test/ -
http://test.janbuschtoens.de/test/ 重写为
/test/
这条规则似乎被忽略了。
【问题讨论】:
-
1) 你能告诉我你想达到什么目标吗?请展示几个示例(从 url => 到 url)。 2)这个 .htaccess 位于哪里 - 我猜在根文件夹中,而您在子域文件夹中没有这样的文件?
-
好的,我有足够的信息。我只需要找点时间去做。希望我很快就会有一些空闲时间,否则我将不得不在下班后(在 6 小时左右)回来。我希望你能等:)
标签: apache .htaccess mod-rewrite subdomain