I am having an issue trying to change my rewrite rule to use https.

Before change, I had the following:

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)$ http://example.com/api/$1 [L,P]

Which redirected all the requests with 'api' to my server.

Now, I've changed http to https:

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)$ https://example.com/api/$1 [L,P]

And when I am testing this, it throws:

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.

Error 500

What am I doing wrong here?

My .htaccess is:

Options -Indexes +FollowSymLinks -MultiViews
# BEGIN Expire headers
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 5 seconds"
    ExpiresByType image/x-icon "access plus 2592000 seconds"
    ExpiresByType image/jpeg "access plus 2592000 seconds"
    ExpiresByType image/png "access plus 2592000 seconds"
    ExpiresByType image/gif "access plus 2592000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
    ExpiresByType text/css "access plus 604800 seconds"
    ExpiresByType text/javascript "access plus 648000 seconds"
    ExpiresByType application/javascript "access plus 648000 seconds"
    ExpiresByType application/x-javascript "access plus 648000 seconds"
    ExpiresByType text/html "access plus 6000 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
# END Expire headers

RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)$ https://example.com/api/$1 [L,P]

# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]

# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

This is what works for me..

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^api/(.*)$ /$1 [R=301,NC,L]

so basically is takes example.com/api and results in https://example.com but the real location that it loads is in fact https://otherdomain.com/api

Update: I belive you also want to mask the url, to show example.com and not otherexpample.com.

For masking:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC]
RewriteRule ^api/(.*)$ /$1 [R=301,L]
  • 2
    Thank you for you answer. However, In my case, it should take example.com/api and result other-domain.com/api – uksz Nov 24 '16 at 9:30
  • that is exactly what it does with /api just hidden..if you want the /api to show in the url then use api/$1 in the last line. – norcal johnny Nov 24 '16 at 9:32
  • 2
    norcal, this works great! however, the requests are visible to the user - he sees that the requests go to otherdomain.com (they are redirected, not proxied). How can I make them invisible (my original version works like that) – uksz Nov 24 '16 at 9:39
  • 1
    "mask the url" - but you can't simply use an internal rewrite (like you have suggested) when rewriting to an entirely different host (this will implicitly trigger an external redirect). (However, you aren't actually specifying the "other host" in the substitution.) You would need to use mod_proxy (the P flag in the RewriteRule) which is what the OP is doing. – MrWhite Nov 24 '16 at 10:42

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.