2

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"
</IfModule>
# 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]
3

1 Answer 1

1

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]
4
  • 2
    Thank you for you answer. However, In my case, it should take example.com/api and result other-domain.com/api
    – uksz
    Commented Nov 24, 2016 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. Commented Nov 24, 2016 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
    Commented Nov 24, 2016 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
    Commented Nov 24, 2016 at 10:42

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.