在重写规则重用子域 [英] Reuse subdomain in a rewrite rule
问题描述
我有点坚持我的重定向规则。小例子比长篇演说更好,在这里不用大丑:
I'm a bit stuck with my redirect rule. Little example better than a long speech, here goes the Great Ugliness:
IndexIgnore *
ErrorDocument 404 http://www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^([^\.]+)\.example\.com$ http://example.com/private/$1/public/si.php [L]
我的目标是让子域([^ \。] +)
,并用它在重定向,而不是 $ 1
。
例如, test.example.com
应重定向到 http://example.com/private/test/public/si.php
My objective is to get the subdomain ([^\.]+)
and use it in the redirection instead of $1
.
For example, test.example.com
should redirect to http://example.com/private/test/public/si.php
感谢您的任何帮助。
问候, 秒。
最终工作htaccess的:
final working htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/private/
RewriteCond %{HTTP_HOST} ([^\.]+).example.com [NC]
RewriteRule ^ /private/%1/public/$1 [R=301,L]
重定向 anysub.example.com/anypage
到 anysub.example.com/private/anysub/public/anypage
推荐答案
用于匹配的模式的重写规则
将从来没有的字符串包含主机名,仅URI和没有任何查询字符串。如果你忽略实际URI是什么,那么你并不需要在重写规则
模式匹配任何东西。你需要使用的%符号反向引用一个previous在一个分组的RewriteCond
The string used to match against the pattern of a RewriteRule
will never contain the hostname, only the URI and without any query string. If you are to ignore what the actual URI is, then you don't need a pattern in the RewriteRule
to match anything. You'd need to use a % symbol to backreference a previous grouping in a RewriteCond
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^ http://example.com/private/%1/public/si.php [L]
您可以在规则的方括号中添加 R = 301
标志,使永久重定向
You can add a R=301
flag in the rule's square brackets to make the redirect permanent.
这篇关于在重写规则重用子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!