htaccess 子目录到子域和子域到子目录重定向问题

发布于 2024-10-23 13:33:26 字数 854 浏览 1 评论 0原文

我正在尝试完成以下任务:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2024-10-30 13:33:26

只是为了澄清您在原始问题中所说的您需要此重定向:

   http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)

但后来在您的评论中您建议:

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 文件中尝试此操作:

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

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

Just to clarify in your original question you said you need this redirection:

   http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)

But later in your comment you suggested:

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)

Assuming your comment are right, please try this in your .htaccess file:

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

This will redirect www.example.com/site/foo/bar* to foo.example.com/bar* with 301 status to the browser.

南风起 2024-10-30 13:33:26

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

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

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.

# 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文