How to Rewrite URLs in HTTP Requests in An Application with php-buildpack
search cancel

How to Rewrite URLs in HTTP Requests in An Application with php-buildpack

book

Article ID: 297592

calendar_today

Updated On:

Products

VMware Tanzu Application Service for VMs

Issue/Introduction

This article describes how to rewrite URLs in HTTP requests using Apache Module mod_rewrite and .htaccess with php-buildpack.

Note that php-buildpack also supports NGINX as its web server program, but that is not covered in this article.

 


Environment


Resolution

  • Make sure that you have httpd as web server of your application, which is the default setting of php-buildpack. 
  • (Optional) In order to debug or trace the rewrite rules, you can increase the log level of rewrite engine by changing logging configuration as described in, http://docs.pivotal.io/pivotalcf/1-7/buildpacks/php/gsg-php-config.html#engine-configurations
    • Copy the default httpd-logging.conf configuration to your local .bp-config/httpd/extra/
    • Append rewrite:trace6 to the line that begins with LogLevel
    • The following one liner script will automatically do the above:
    • mkdir -p .bp-config/httpd/extra && curl https://raw.githubusercontent.com/cloudfoundry/php-buildpack/master/defaults/config/httpd/extra/httpd-logging.conf > .bp-config/httpd/extra/httpd-logging.conf && sed -ie "s/LogLevel \([[:alpha:]]*\)/LogLevel \1 rewrite:trace6/" .bp-config/httpd/extra/httpd-logging.conf
    • Add rewrite settings in .htaccess in your top directory. The following example will rewrite URI to index.php with the original URL saved in query parameter req:
    RewriteEngine On
    RewriteBase /
    RewriteCond "%{REQUEST_URI}" "!=/index.php"
    RewriteRule "^(.*)" "/index.php?req=$1" [L,PT]
    
    
    • In the following example of "cf logs", you can see that a request to /aaa was rewritten to /index.php?req=aaa
    2016-09-16T17:26:56.11+0900 [RTR/4]      OUT ika.cfapps.io - [16/09/2016:08:26:56.111 +0000] "GET /aaa HTTP/1.1" 200 0 65 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" 10.10.66.113:53732 x_forwarded_for:"211.15.213.33" x_forwarded_proto:"http" vcap_request_id:be085190-4590-4196-4592-8ced2f9e9bcb response_time:0.004164426 app_id:cf221de1-2d8a-4d5e-8904-16c477a92459 index:0
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095329 2016] [rewrite:trace3] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] strip per-dir prefix: /home/vcap/app/htdocs/aaa -> aaa
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095419 2016] [rewrite:trace3] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] applying pattern '^(.*)' to uri 'aaa'
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095448 2016] [rewrite:trace4] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] RewriteCond: input='/aaa' pattern='!=/index.php' => matched
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095456 2016] [rewrite:trace2] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] rewrite 'aaa' -> '/index.php?req=aaa'
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095462 2016] [rewrite:trace3] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] split uri=/index.php?req=aaa -> uri=/index.php, args=req=aaa
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095468 2016] [rewrite:trace2] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] forcing '/index.php' to get passed through to next API URI-to-filename handler
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095477 2016] [rewrite:trace2] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] trying to replace prefix /home/vcap/app/htdocs/ with /
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095483 2016] [rewrite:trace2] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] trying to replace context docroot /home/vcap/app/htdocs with context prefix
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095488 2016] [rewrite:trace1] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe70002970/initial] [perdir /home/vcap/app/htdocs/] internal redirect with /index.php [INTERNAL REDIRECT]
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095563 2016] [rewrite:trace3] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe68003298/initial/redir#1] [perdir /home/vcap/app/htdocs/] strip per-dir prefix: /home/vcap/app/htdocs/index.php -> index.php
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095572 2016] [rewrite:trace3] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe68003298/initial/redir#1] [perdir /home/vcap/app/htdocs/] applying pattern '^(.*)' to uri 'index.php'
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095583 2016] [rewrite:trace4] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe68003298/initial/redir#1] [perdir /home/vcap/app/htdocs/] RewriteCond: input='/index.php' pattern='!=/index.php' => not-matched
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | [Fri Sep 16 08:26:56.095590 2016] [rewrite:trace1] [pid 60:tid 140456235104000] mod_rewrite.c(477): [client 211.15.213.33:31558] 211.15.213.33 - - [ika.cfapps.io/sid#e023b0][rid#7fbe68003298/initial/redir#1] [perdir /home/vcap/app/htdocs/] pass through /home/vcap/app/htdocs/index.php
    2016-09-16T17:26:56.11+0900 [APP/0]      OUT 08:26:56 httpd   | 211.15.213.33 - - [16/Sep/2016:08:26:56 +0000] "GET /aaa HTTP/1.1" 200 65 vcap_request_id=be085190-4590-4196-4592-8ced2f9e9bcb peer_addr=10.
    
     

    Impact

    It is recommended to revert the log setting once you are done with the rewrite settings as it may impact the performance of your application.