htaccess 子目录到子域和子域到子目录重定向问题
我正在尝试完成以下任务:
http:// www.example.com/site/abc 并使用 http 301 重定向到子域 http:// abc.example.com 然后再次返回 Apache: http://abc.example.com --> /site/abc
我希望在根文件夹的 .htaccess 中定义两个重定向。
我尝试了几种组合,但不幸的是没有任何运气。这就是我现在所拥有的:
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L]
# 3. internal redirect to the corresponding directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC]
我收到了 500 服务器错误。
提前致谢!
I'm trying to accomplish the following:
http:// www.example.com/site/abc with a http 301 redirect to subdomain http:// abc.example.com
and back again within Apache:
http:// abc.example.com --> /site/abc
I want both redirects to be defined in the .htaccess in the root folder.
I've tried several combinations, but unfortunately without any luck. This is what I have now:
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L]
# 3. internal redirect to the corresponding directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC]
I receive a 500 server error instead.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了澄清您在原始问题中所说的您需要此重定向:
但后来在您的评论中您建议:
假设您的评论是正确的,请在您的 .htaccess 文件中尝试此操作:
这将重定向
www.example.com/site /foo/bar*
到foo.example.com/bar*
,浏览器状态为 301。Just to clarify in your original question you said you need this redirection:
But later in your comment you suggested:
Assuming your comment are right, please try this in your .htaccess file:
This will redirect
www.example.com/site/foo/bar*
tofoo.example.com/bar*
with 301 status to the browser.假设
/site/abc/xyz/part
是磁盘上的实际物理文件,请尝试以下操作(如果实际文件有某些扩展名,则附加它)。还要添加 QSA 标志,以便附加查询字符串。
Assuming
/site/abc/xyz/part
is a actual physical file on disk try following (if actual file got some extension then append it).Also add QSA flag so that query string is appended.