mod_rewrite To Direct Subdomain Access
We are setting up an API and we want to use Apache mod_rewrite to direct all accesses to http://api.domain.com to the script located at /cgi-bin/api.pl. H开发者_开发问答ere is what we're currently using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^(.*)$ /cgi-bin/api.pl [NC,L,QSA]
However, this is giving us the following error:
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
[Fri Sep 03 14:18:32 2010] [error] [client 67.180.34.0] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If we try to access http://domain.com/cgi-bin/api.pl the script functions properly. What are we doing wrong? Please help! Thanks in advance.
I assume that you're defining your rules in a .htaccess
file, where the L
flag might not work like you were expecting.
Since your test pattern ^(.*)$
matches whatever input is given to it, the URL you rewrite to is also matched on the subsequent request, and you get an internal infinite redirection loop (resulting eventually in the server error).
A solution is to check to see if you've already rewritten the URL:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/cgi-bin/api.pl
RewriteCond %{HTTP_HOST} ^api.domain.com$ [NC]
RewriteRule ^.*$ /cgi-bin/api.pl [NC,L,QSA]
精彩评论