Sharlene Delaney
Sharlene Delaney

Reputation: 11

Server Redirect With Ports?

I don't have much experience with Apache or servers, so my apologies if this is a stupid question. I am trying to redirect http://example.com:5557 to http://203.0.113.111:5557. Is this something I can do in the .htaccess file and, if so, what would that look like?

Here is my idea:

Redirect 301 "http://example.com:5557" "http://203.0.113.111:5557"

Upvotes: 1

Views: 226

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Yes, you can do this in .htaccess, but not with the mod_alias Redirect directive. The Redirect directive matches against the URL-path only. You need to match against the HTTP Host request header, which contains the hostname + port number.

Try the following using mod_rewrite instead:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com:5557$ [NC]
RewriteRule ^$ http://203.0.113.111:5557/ [R=301,L]

Note that this redirects the document root (homepage) only, as stated in your example. If you need to redirect any URL to the same URL at the target domain then use the following RewriteRule instead:

:
RewriteRule ^ http://203.0.113.111:5557%{REQUEST_URI} [R=301,L]

NB: Test first with 302 (temporary) redirect to avoid potential caching issues.

Upvotes: 1

Related Questions