0

The canonical website URL will be https://www.example.com

Typically any requests eg for https://example.com will be redirected to https://www.example.com by WordPress site settings.

However, I have a special requirement from a team member regarding an email server need.

Specifically they need any requests made to this URL: https://example.com/foo to be redirected to https://foo.example.com.

To avoid some kind of weird chain or loop happening (or even slowness), I was thinking of placing a redirect rule somewhere near the top of the .htaccess file (ie above the other rules) to facilitate this particular redirect before anything else happens.

a) Would that work, and b) would this code be an acceptable way of doing it?

# BEGIN WordPress

RewriteEngine On

#my code
Redirect 301 /foo https://foo.example.com

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
3
  • Do example.com and foo.example.com point to different hosts (different file systems)? "https://example.com/foo to be redirected" - As well as www.example.com? What about URLs of the form /foo/<something>? What is consuming this redirect? Is it a "browser" or some API call or sometihng?
    – MrWhite
    Mar 20, 2021 at 10:21
  • Thanks for the question. foo.example.com will be used by IT dept for a Microsoft Exchange thing (I don't fully understand it, but they say they need requests to example.com/foo redirected to foo.example.com.
    – Drewdavid
    Mar 22, 2021 at 1:27
  • "used by IT dept for a Microsoft Exchange thing" - that almost sounds like the request should perhaps be forwarded "internally", rather than an external "redirect"?
    – MrWhite
    Mar 25, 2021 at 12:17

1 Answer 1

2

a) Would that work, and b) would this code be an acceptable way of doing it?

Yes.

But you should put the custom directive (the Redirect line) above the BEGIN marker, i.e. the # BEGIN WordPress line, because if not, then your custom directive will be gone when the WordPress rewrite rules are flushed/regenerated.

So:

# your code here:
Redirect 301 /foo https://foo.example.com

# BEGIN WordPress
RewriteEngine On
# ... WordPress rewrite rules here.
# END WordPress

But if the subdomain is in a directory in the document root of the main domain (example.com), then you would want to use the following which prevents foo.example.com/foo from being redirected to foo.example:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^foo/?$ https://foo.example.com/ [L,R=301,NC,QSA]

# BEGIN WordPress
RewriteEngine On
# ... WordPress rewrite rules here.
# END WordPress

And if you wanted to redirect anything in example.com/foo, e.g. example.com/foo/some-file to foo.example.com/some-file, then you can change the above RewriteRule line to: (i.e. You basically just need to play with the rewrite rule's regular expression.)

RewriteRule ^f(/(.*))?$ https://foo.example.com/$2 [L,R=301,NC,QSA]
7
  • Thanks Sally I will have to try this out and get back to you!
    – Drewdavid
    Mar 20, 2021 at 4:22
  • 1
    "to prevent foo.example.com/foo from redirecting" - The RedirectMatch directive as posted won't prevent that.
    – MrWhite
    Mar 20, 2021 at 10:23
  • Thank you @MrWhite. I was confused with my own tests..
    – Sally CJ
    Mar 20, 2021 at 15:46
  • 1
    @Drewdavid, I've corrected the mistake in my answer, but then I made a small typo in the RewriteRule ^f( - that "f" should be "foo".. and I'll correct that, but after I received your reply.
    – Sally CJ
    Mar 20, 2021 at 16:02
  • 1
    I would certainly go with the mod_rewrite solution (+1), rather than the mod_alias Redirect directive here. Whilst the Redirect as posted "works" in a browser context, it is dependent on the browser "fixing" the target URL, since the slash after the target hostname is missing. (From comments above, it is not clear how this redirect is being used... "used by IT dept for a Microsoft Exchange thing") The Redirect directive is also prefix-matching, so /foo/bar will be redirected to https://foo.example.com/bar, which may or may not be desirable.
    – MrWhite
    Mar 25, 2021 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.