简体   繁体   English

.htaccess中的子域和路径的RewriteCond问题

[英]RewriteCond issue with subdomains and paths in .htaccess

I have three different domains pointing to the same WordPress's host instance depending on the language (managed with the qTranslate plugin): 根据语言(由qTranslate插件管理),我有三个不同的域指向同一WordPress的主机实例:

RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.cat$
RewriteRule ^/?$ "http\:\/\/ca\.mydomain\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule ^/?$ "http\:\/\/en\.mydomain\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.es$
RewriteRule ^/?$ "http\:\/\/es\.mydomain\.com\/" [R=301,L]

With that I've managed to achieve the following redirects: 这样,我设法实现了以下重定向:

  • mydomain.cat or www.mydomain.cat ---> ca .mydomain.com ---> Shows website in catalan mydomain.cat或www.mydomain.cat ---> ca .mydomain.com --->显示加泰罗尼亚语的网站
  • mydomain.es or www.mydomain.es ---> es .mydomain.com ---> Shows website in spanish mydomain.es或www.mydomain.es ---> es .mydomain.com --->以西班牙语显示网站
  • mydomain.com or www.mydomain.com ---> en .mydomain.com ---> Shows website in english mydomain.com或www.mydomain.com ---> en .mydomain.com --->以英文显示网站

Now the trouble I have is that, for example, user types mydomain.cat/work it doesn't works fine, it goes to mydomain.com/work/ and the language part in the subdomain is missing. 现在我遇到的麻烦是,例如,用户键入mydomain.cat/work不能正常工作,它转到mydomain.com/work/,子域中的语言部分丢失了。 It would be perfect if, in that case, it redirects to ca.mydomain.com/work. 如果那样的话,它将重定向到ca.mydomain.com/work,那将是完美的。

I've researched and tried to modify the three RewriteCond rules to "add" the additional content at the end of the url (don't know if is better with QUERY_STRING or with parameters) if it exists, but I couldn't get it working fine, rather I don't know if it's the best way to achieve it. 我已经研究并尝试修改三个RewriteCond规则,以在URL的末尾“添加”其他内容(不知道使用QUERY_STRING或使用参数是否更好),但是存在,但是我无法获取工作正常,但我不知道这是否是实现此目标的最佳方法。

Anyone can help me or give me a clue? 任何人都可以帮助我或提供提示吗?

Ok, I got it working: 好的,我可以正常工作了:

RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.cat$
RewriteRule (.*) "http\:\/\/ca\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule (.*) "http\:\/\/en\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.es$
RewriteRule (.*) "http\:\/\/es\.mydomain\.com\/$1" [R=301,L]

I added the $1 at the end of the RewriteRule statements and overwrote the ^/?$ at the beginning with (.*). 我在RewriteRule语句的末尾添加了$ 1,并以(。*)开头改写了^ /?$。 I also added these two lines above the others: 我还在其他两行之上添加了这两行:

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

To prevent the trailing slash. 防止尾随斜杠。 I hope this is helpful for somebody. 我希望这对某人有帮助。

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

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