我正在努力完成以下工作:
带有http 301的http:// example.com /site/abc重定向到子域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服务器错误。
提前感谢!
发布于 2011-03-17 23:56:01
只是为了澄清你最初的问题,你说你需要这个重定向:
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]这会将状态为301的foo.example.com/bar*的www.example.com/site/foo/bar*重定向到浏览器。
发布于 2011-03-17 23:59:15
假设/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]https://stackoverflow.com/questions/5339454
复制相似问题