将子域重定向到子目录 - 尾部斜杠

时间:2011-10-03 05:20:21

标签: apache .htaccess subdomain

我想将[abc] .apps.example.com重定向到以下apps.example.com/[abc],但仍然显示在网址中使用[abc] .apps.example.com。

[abc]是一个例如。

calendar.apps.example.com books.apps.example.com 等

通过一些挖掘和调整,我有以下设置但是当缺少结束斜杠时不起作用:

RewriteEngine On
RewriteBase /

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^www\.apps\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.apps\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.apps\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.apps\.example\.com$ [NC]
RewriteRule ^.*$ /%2/$0 [QSA,L]

输入以下内容时:
calendar.apps.example.com/showcalendar/
它被正确地重定向到:apps.example.com/calendar/showcalendar /

但是当缺少尾随斜线时 calendar.apps.example.com/showcalendar

我收到404错误说/ calendar / calendar / showcalendar / not found。

如果您知道上述代码有什么问题或者有更好的解决方案,请告诉我们。

干杯,

1 个答案:

答案 0 :(得分:1)

尝试使用以下重写规则来添加尾部斜杠。

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]