I need to redirect www.example.com:81/site/ to www.example.com. I have used mod_rewrite a few times, is it possible to solve this in a similar way?

  • Make sure you use a technique that returns a 301 Permanent Redirect as I might cause browsers to update their bookmarks!
    – bot47
    Mar 24 '14 at 8:48
  • But as noted in comments (below): "www.example.com:81/site/ doesn't exist anymore."
    – MrWhite
    May 1 '14 at 13:48

Below is the code you can use in .htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://example.com:81/$1 [P]

On example.com:81 create .htaccess file (where site/ is):

RewriteEngine on
RewriteRule  (.*)  http://example.com:80/$1  [P,L] 

With this when you go to example.com:81 the site will proxy to example.com.

  • 1
    I can't create .htaccess file on www.example.com:81/site/. The site doesn't exist anymore. Just www.example.com.
    – user37194
    Mar 23 '14 at 15:03

This might do what you are looking for:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^81$   # check if port 81
RewriteCond %{REQUEST_URI} ^site/ # check if it starts with "site/"
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

[R=301,L] if you want permanent redirect (the :81 taken out of SEO results for example).
Im not really sure if you have to add :80 after the %{SERVER_NAME}, please let me know so I can update the answer.


You need to have a web server running at www.example.com:81, and create a .htaccess file there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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