htaccess 重写子域名和请求 uri

htaccess 重写子域名和请求 uri

我的 htaccess 中的重写条件有问题。我需要从子域重定向请求。但是,每种语言有三个子域:en.foo.com、es.foo.com、www.foo.com。如果他们有请求,他们必须保留相应的子域。

例如:

要求:
fdsfds.foo.com/测试

期望: www.foo.com/test

现在: www.foo.com

.htaccess 文件:

# BEGIN .net to .com redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} foo.net$ [NC]
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
# END .net to .com redirect

# BEGIN force www on non-www requests
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# END force www on non-www requests

# BEGIN .net to .com redirect (english)
RewriteCond %{HTTP_HOST} ^en\.foo\.net$ [NC]
RewriteRule ^(.*)$ http://en.foo.com/$1 [R=301,L]
# END .net to .com redirect (english)

# BEGIN .net to .com redirect (spanish)
RewriteCond %{HTTP_HOST} ^es\.foo\.net$ [NC]
RewriteRule ^(.*)$ http://es.foo.com/$1 [R=301,L]
# END .net to .com redirect (spanish)

# BEGIN .net to .com redirect (spanish)
RewriteCond %{HTTP_HOST} ^www\.foo\.net$ [NC]
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
# END .net to .com redirect (spanish)

# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^es\. [NC]
RewriteCond %{HTTP_HOST} !^en\. [NC]
RewriteRule ^ http://www.foo.com/ [L,R=301]

答案1

好吧,我认为你只需要修改最后一个 RewriteRule,它将所有其他请求(即不以 es 或 en 或 www 开头的请求)重定向到http://www.foo.com/。你可以尝试这样做吗:

RewriteRule ^(.*)$ http://www.foo.com/$1 [L,R=301]

如果它做了正确的事情,请告诉我。

相关内容