Issue
On Ubuntu 14.04, using Apache 2.4.7, I'm trying to redirect all of the HTTP requests in a subdomain to a single PHP file.
For example, that the request api.exampleapp.com/auth/login
will be redirected to /var/www/example-api/api.php?q=auth/login
All of the code is located at:
/var/www/example-api
In this directory is a single api.php script that handles everything, and other libraries and resources that should not be publicly available.
This is the configuration in /etc/apache2/sites-available/api.exampleapp.com.conf
:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/example-api
ServerName api.exampleapp.com
<Directory "/var/www/example-api">
Options -Indexes
RewriteEngine On
LogLevel alert rewrite:trace6
RewriteBase /
RewriteRule ^(.*)$ api.php?q=$1 [QSA,L]
</Directory>
</VirtualHost>
But when I'm requesting /var/www/example-api
, I get a 500 response.
This is the output I get in error_log
(irrelevant data like times was removed):
[rid#a0/initial] [perdir /var/www/example-api/] add path info postfix: /var/www/example-api/auth -> /var/www/example-api/auth/login
[rid#a0/initial] [perdir /var/www/example-api/] strip per-dir prefix: /var/www/example-api/auth/login -> auth/login
[rid#a0/initial] [perdir /var/www/example-api/] applying pattern '^(.*)$' to uri 'auth/login'
[rid#a0/initial] [perdir /var/www/example-api/] rewrite 'auth/login' -> 'api.php?q=auth/login'
[rid#a0/initial] split uri=api.php?q=auth/login -> uri=api.php, args=q=auth/login
[rid#a0/initial] [perdir /var/www/example-api/] add per-dir prefix: api.php -> /var/www/example-api/api.php
[rid#a0/initial] [perdir /var/www/example-api/] trying to replace prefix /var/www/example-api/ with /
[rid#a0/initial] strip matching prefix: /var/www/example-api/api.php -> api.php
[rid#a0/initial] add subst prefix: api.php -> /api.php
[rid#a0/initial] [perdir /var/www/example-api/] internal redirect with /api.php [INTERNAL REDIRECT]
[rid#20/initial/redir#1] [perdir /var/www/example-api/] strip per-dir prefix: /var/www/example-api/api.php -> api.php
[rid#20/initial/redir#1] [perdir /var/www/example-api/] applying pattern '^(.*)$' to uri 'api.php'
[rid#20/initial/redir#1] [perdir /var/www/example-api/] rewrite 'api.php' -> 'api.php?q=api.php'
[rid#20/initial/redir#1] split uri=api.php?q=api.php -> uri=api.php, args=q=api.php&q=auth/login
[rid#20/initial/redir#1] [perdir /var/www/example-api/] add per-dir prefix: api.php -> /var/www/example-api/api.php
[rid#20/initial/redir#1] [perdir /var/www/example-api/] initial URL equal rewritten URL: /var/www/example-api/api.php [IGNORING REWRITE]
How do I prevent the internal redirect after the rewrite?
I tried adding RewriteCond %{REQUEST_URI} !/api.php
and slightly other variations, but nothing worked.
Solution
Add before RewriteRule
:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Not rewrite real file or folder
Answered By - Croises Answer Checked By - Marilyn (WPSolving Volunteer)