简体   繁体   English

将子域重写到文件夹,然后将该文件夹重定向到子域

[英]Rewrite subdomain to a folder and redirect that folder to a subdomain

I have rewrite rule that sends all users from random.example.com to /random/ 我有一个重写规则,将所有用户从random.example.com发送到/random/

But I want to make it so that IF a user was to directly visit /random/ it would redirect (redirect NOT rewrite) them to the subdomain. 但是我想这样做,以便如果用户要直接访问/random/ ,它将把他们重定向(重定向到非重写)到子域。

This is what I have: 这就是我所拥有的:

RewriteEngine on  
# NO WWW
RewriteCond %{HTTP_HOST} ^www.example.com [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# USE RANDOM SUB DOMAIN WITH CONTENT IN SUB FOLDER
RewriteCond %{HTTP_HOST} random.example.com$
RewriteCond %{REQUEST_URI} !^/random
RewriteRule ^(.*)$ /random/$1 [L]

# SEND SNOOPERS AT SUB FOLDER TO SUB
Redirect ^(.*)$/random/$1 %{HTTP_HOST}random.example.com$

However, I get a server 500 error for that last line. 但是,对于最后一行,我收到服务器500错误。 Basically it needs to handle any urls that come through on the random directory, so for example: 基本上,它需要处理随机目录中出现的所有网址,例如:

http://example.com/random -> http://random.example.com/

http://example.com/random/fakeplace/ -> http://random.example.com/fakeplace/

http://example.com/random?id=4 -> http://random.example.com/?id=4

Your syntax is wrong with the last line. 您的语法在最后一行是错误的。 You are even mixing RewriteRule with Redirect. 您甚至在混合RewriteRule和Redirect。 Try these rules. 尝试这些规则。 I changed a few things. 我改变了几件事。

RewriteEngine on  
# NO WWW
RewriteCond %{HTTP_HOST} ^www.example.com [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# USE RANDOM SUB DOMAIN WITH CONTENT IN SUB FOLDER
RewriteCond %{HTTP_HOST} ^random\.example\.com$
RewriteRule !^random/? random%{REQUEST_URI} [NC,L]

RewriteCond %{THE_REQUEST} \s/random/([^\s]*) [NC]
RewriteRule ^ http://random.example.com/%1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM