简体   繁体   中英

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)

So I'm trying to redirect anyone using incorrect URLs to the correct TLD with the gm subfolder. 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/

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:

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

Your htaccess is full of syntax errors.

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

to

  • example.com/gm

Clear your browser's cache before testing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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