首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >htaccess subdir to subdomain和subdomain to subdir重定向问题

htaccess subdir to subdomain和subdomain to subdir重定向问题
EN

Stack Overflow用户
提问于 2011-03-17 21:11:21
回答 2查看 980关注 0票数 1

我正在努力完成以下工作:

带有http 301的http:// example.com /site/abc重定向到子域http:// abc.example.com,然后返回Apache: http:// abc.example.com --> /site/abc

我希望两个重定向都在根文件夹的.htaccess中定义。

我尝试了几种组合,但不幸的是没有任何运气。这就是我现在所拥有的:

代码语言:javascript
复制
# 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服务器错误。

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2011-03-17 23:56:01

只是为了澄清你最初的问题,你说你需要这个重定向:

代码语言:javascript
复制
   http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)

但在你后来的评论中,你建议:

代码语言:javascript
复制
http://www.example.com/site/abc/xyz/part?id=123&name=lmn => http://abc.example.com/xyz/part?id=123&name=lmn (**site/abc missing** from destination URL)

假设您的评论是正确的,请在您的.htaccess文件中尝试以下内容:

代码语言:javascript
复制
RewriteEngine On    
RewriteCond %{HTTP_HOST}   ^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule site/(.+)/(.*) http://$1.example.com/$2 [R=301,L]

这会将状态为301的foo.example.com/bar*www.example.com/site/foo/bar*重定向到浏览器。

票数 1
EN

Stack Overflow用户

发布于 2011-03-17 23:59:15

假设/site/abc/xyz/part是磁盘上的一个实际物理文件,请尝试以下操作(如果实际文件具有某些扩展名,则附加它)。还要添加QSA标志,以便附加查询字符串。

代码语言:javascript
复制
# 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,QSA]

# 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,QSA]

# 3. internal redirect to the corresponding directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC,QSA]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5339454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档