简体   繁体   English

如果域不相等并且子文件夹是此文件,则htaccess重写URL

[英]htaccess rewrite URL if domain doesn't equal and subfolder is this

I'm trying to get a rewrite working and need a hand... 我正在尝试进行重写并需要帮助...

I've got a set of websites, which domains are like this 我有一组网站,这些域名是这样的

example.com
example.co.uk
example.ie
example.com/gm

The way the site is setup means that all domains can display the .com/gm content (GM doesn't have it's own domain and we want it only served from the .com domain) 网站设置的方式意味着所有域都可以显示.com / gm内容(GM没有它自己的域,我们希望仅从.com域提供它)

So I'm trying to redirect anyone using incorrect URLs to the correct TLD with the gm subfolder. 因此,我尝试使用gm子文件夹将使用错误URL的任何人重定向到正确的TLD。 EG: 例如:

going to www.example.co.uk/gm would 301 to www.example.com/gm

I've currently got this: 我目前有这个:

RewriteCond %!{HTTP_HOST}:{REQUEST_URI} !^example.com:gm$ [L,R]
RewriteRule ^(.*)$ https://www.example.com/gm$1 [R=301,L]

Which obviously isn't happy 哪个显然不开心

Appreciate a point in the right direction, basically from what I can tell I need a rule that checks the domain doesn't equal .com if they're trying to get to /gm/ and redirects them to .com/gm/ 基本上,从正确的方向来看,我可以告诉我,我需要一条规则来检查域是否等于.com(如果他们试图访问/ gm /并将其重定向到.com / gm /)

Thanks!! 谢谢!!

It is easier to check if the /gm/ path was requested in the RewriteRule, and prefix that with a RewriteCond that checks for the host name – something like this should do it: 在RewriteRule中检查是否请求了/gm/路径,并在其前面加上一个用于检查主机名的RewriteCond,这样比较容易-可以这样做:

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(gm/.*)$ https://www.example.com/$1 [R=301,L]

Your htaccess is full of syntax errors. 您的htaccess充满语法错误。

Try the following: 请尝试以下操作:

RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk$
RewriteCond %{REQUEST_URI} ^/gm/
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

This will redirect 这将重定向

  • example.co.uk/gm example.co.uk/gm

to

  • example.com/gm example.com/gm

Clear your browser's cache before testing this. 在测试之前,请清除浏览器的缓存。

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

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