Saturday 7 January 2012

How to Debug Rewrite Rules with Apache


 
How to Debug Rewrite Rules with Apache

RewriteLog "/myfolder/mylogfile.log" 
RewriteLogLevel 3
 
Please note that:
 
  • it can slow down your server a lot
  • you should not leave this in production for long (even with RewriteLogLevel set to 0 – instead, remove both directives)
If you want to learn more about these two directives:

Original Post:http://blog.nexcess.net/2011/12/19/debugging-apache-rewrites-and-redirects/

with the left angle bracket (<) indicating that this is a response from the server (right angle brackets (<) are commands sent by the client -- curl, in this case)

senseexcept-lm:~ hitesha$ curl --verbose --head --location www.yahoo.com
 * About to connect() to www.yahoo.com port 80 (#0)*   Trying 98.139.180.149... connected
* Connected to www.yahoo.com (98.139.180.149) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
> Host: www.yahoo.com
> Accept: */*
>
< HTTP/1.1 302 Found
HTTP/1.1 302 Found
< Date: Sat, 07 Jan 2012 16:40:13 GMT
Date: Sat, 07 Jan 2012 16:40:13 GMT
< P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
< Cache-Control: private
Cache-Control: private
< Set-Cookie: IU=deleted; expires=Fri, 07-Jan-2011 16:40:12 GMT; path=/; domain=.yahoo.com
Set-Cookie: IU=deleted; expires=Fri, 07-Jan-2011 16:40:12 GMT; path=/; domain=.yahoo.com
< Set-Cookie: fpc=d=62nSIYYXWh.YRIFs4999Ph1zERHiOCTvaBLdbi5i3LWiDkwct_jexHLg8L_bfL0jsECYnDKSrGhBAGve9VycrVM9N.QAZWBMuCtDTeYY9qkv8.xVvJvmNb9XFHgeBwNQSsY7w94U7_.lOvoisDvarPneTNMe8nP8toplokK_sV84ogfdqmCCxOCe4af6wI3MmZfTKF8-&v=2; expires=Sun, 06-Jan-2013 16:40:13 GMT; path=/; domain=www.yahoo.com
Set-Cookie: fpc=d=62nSIYYXWh.YRIFs4999Ph1zERHiOCTvaBLdbi5i3LWiDkwct_jexHLg8L_bfL0jsECYnDKSrGhBAGve9VycrVM9N.QAZWBMuCtDTeYY9qkv8.xVvJvmNb9XFHgeBwNQSsY7w94U7_.lOvoisDvarPneTNMe8nP8toplokK_sV84ogfdqmCCxOCe4af6wI3MmZfTKF8-&v=2; expires=Sun, 06-Jan-2013 16:40:13 GMT; path=/; domain=www.yahoo.com
< Location: http://in.yahoo.com/?p=us
Location: http://in.yahoo.com/?p=us
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Age: 0
Age: 0
< Connection: keep-alive
Connection: keep-alive
< Server: YTS/1.20.0
Server: YTS/1.20.0

<
* Connection #0 to host www.yahoo.com left intact
* Issue another request to this URL: 'http://in.yahoo.com/?p=us'
* Disables POST, goes with HEAD
* About to connect() to in.yahoo.com port 80 (#1)
*   Trying 121.101.152.229... connected
* Connected to in.yahoo.com (121.101.152.229) port 80 (#1)
> HEAD /?p=us HTTP/1.1
> User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
> Host: in.yahoo.com
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 07 Jan 2012 16:40:14 GMT
Date: Sat, 07 Jan 2012 16:40:14 GMT
< P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
< Cache-Control: private
Cache-Control: private
< Set-Cookie: IU=deleted; expires=Fri, 07-Jan-2011 16:40:13 GMT; path=/; domain=.yahoo.com
Set-Cookie: IU=deleted; expires=Fri, 07-Jan-2011 16:40:13 GMT; path=/; domain=.yahoo.com
< Set-Cookie: PH=deleted; expires=Fri, 07-Jan-2011 16:40:13 GMT; path=/; domain=.yahoo.com
Set-Cookie: PH=deleted; expires=Fri, 07-Jan-2011 16:40:13 GMT; path=/; domain=.yahoo.com
< Set-Cookie: fpc=d=JgEWsD4UWh.7t0gCxPiBAO3.lhOy5fuIS7.0f6.nKmlAB38s64fQvVZcwcRo43CXVNNumllSE5G_oj7ISZj3n59Ki4xEit.x6r1HIT8N278OalgmLFy_WeTq7TES3v2.1PnqnHTf_AZcLrwclutenqSf4LJ9MO1JNxH8XfV.SwaCQd9LOjM6s5KYpLsH.z1EnESrooY-&v=2; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
Set-Cookie: fpc=d=JgEWsD4UWh.7t0gCxPiBAO3.lhOy5fuIS7.0f6.nKmlAB38s64fQvVZcwcRo43CXVNNumllSE5G_oj7ISZj3n59Ki4xEit.x6r1HIT8N278OalgmLFy_WeTq7TES3v2.1PnqnHTf_AZcLrwclutenqSf4LJ9MO1JNxH8XfV.SwaCQd9LOjM6s5KYpLsH.z1EnESrooY-&v=2; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
< Set-Cookie: fpc=d=S8TNp6AUWh8pruj6562p7VI3GhA.iiDbpEwhF2mxTJ9U2pmN9SKV55DPxOhZtBpQoROkCz2VHLF_1cpqLRnfIIZlumKqgLMKUHAvFVlS_JUP9o6LBdjiAm6f_kvXfpWQQSD6qmR4ZIcRcJl9Oa4LupfpjSE40iyRYATS780m6OW44R94vxiL.Z1mJxnkR37u_IKRk0lX77KPovhShJeRvMftJ3E2MKquhblUaoaIjuStX6JsRAyoO6iRfZJDsP9uv.0_l0rM&v=2; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
Set-Cookie: fpc=d=S8TNp6AUWh8pruj6562p7VI3GhA.iiDbpEwhF2mxTJ9U2pmN9SKV55DPxOhZtBpQoROkCz2VHLF_1cpqLRnfIIZlumKqgLMKUHAvFVlS_JUP9o6LBdjiAm6f_kvXfpWQQSD6qmR4ZIcRcJl9Oa4LupfpjSE40iyRYATS780m6OW44R94vxiL.Z1mJxnkR37u_IKRk0lX77KPovhShJeRvMftJ3E2MKquhblUaoaIjuStX6JsRAyoO6iRfZJDsP9uv.0_l0rM&v=2; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
< Set-Cookie: fpps=_page=%7B%22wsid%22%3A%2221445690%22%7D; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
Set-Cookie: fpps=_page=%7B%22wsid%22%3A%2221445690%22%7D; expires=Sun, 06-Jan-2013 16:40:14 GMT; path=/; domain=in.yahoo.com
< Set-Cookie: fpt=d=aS68.yfXeczppyKttfFELEaqVxoxTepIVTZcCs4QI0YnY9uoGKrAOyc379WQuBVI7iSNhLZ1m20TH9OAxITki79pAece9Khcu8XfmClaXxtWsouOzuPjpAtYyAQVFHuAJI0x0QU8JD2UErJtHiBBBRjlfC.BU.da3aWT3AETz93pXx7LSvj_m9YweDx_UegK5yghY0x6bBFhuAYDr2DrjcJOVE5yGlI5_cdjRpgmkCL.gGks64HkZG1ZLpg8ubutzij9jam.1kmJFLvKleHMksMgJgvDPW4EWA9C.xEK6y9HUsDEzzePZv3jMrCWuHAY23exbqmHG_uYFaiYOAAiTf8vEMZoqCptokVclXu_xz89klL4IvWDj6UQzpxmwaQc.yNL.2QK.hXmgDqRljny1ip2e3b1hF0HtdNZHSSaq2ftU4ynCaIQqhG6negjD8wEu8aXQy8d6FphWtiEYr3GJRrn6UQGD5efQ7dFfeA7yxHv188BeBRsOAA5AgmVZgFDMmgeBXPuFHq5uV4GG9sPkG273Jcn37QU3zLP&v=1; path=/; domain=in.yahoo.com
Set-Cookie: fpt=d=aS68.yfXeczppyKttfFELEaqVxoxTepIVTZcCs4QI0YnY9uoGKrAOyc379WQuBVI7iSNhLZ1m20TH9OAxITki79pAece9Khcu8XfmClaXxtWsouOzuPjpAtYyAQVFHuAJI0x0QU8JD2UErJtHiBBBRjlfC.BU.da3aWT3AETz93pXx7LSvj_m9YweDx_UegK5yghY0x6bBFhuAYDr2DrjcJOVE5yGlI5_cdjRpgmkCL.gGks64HkZG1ZLpg8ubutzij9jam.1kmJFLvKleHMksMgJgvDPW4EWA9C.xEK6y9HUsDEzzePZv3jMrCWuHAY23exbqmHG_uYFaiYOAAiTf8vEMZoqCptokVclXu_xz89klL4IvWDj6UQzpxmwaQc.yNL.2QK.hXmgDqRljny1ip2e3b1hF0HtdNZHSSaq2ftU4ynCaIQqhG6negjD8wEu8aXQy8d6FphWtiEYr3GJRrn6UQGD5efQ7dFfeA7yxHv188BeBRsOAA5AgmVZgFDMmgeBXPuFHq5uV4GG9sPkG273Jcn37QU3zLP&v=1; path=/; domain=in.yahoo.com
< Set-Cookie: fpc_s=d=hVjAbR8UWh.3C6IKs4uB5Br.uhIQuGAdy635M1qsR7rU3lCDZexkFtqPmxIo_NNjWWY.HMGPDLNVNsSLY0hfPspxeAFXlfn_NEUJIuprb6yCx0eEKtShbiiZgT5s_9XEwZ_3IOuCz3Z55NDE8.q1VQHOhpQe80T9TgYCROHmruHPAuuaShN2U8ZtUtLlzuc1iz6EwFnXOzwyYiuOeFylk_vK4wMm36ixCDUuAbdJzmzPHymxOaniTOG78kCf7419PRkJjU8zeBrvCSZ3MKJPDRvmOWSTdDoRLcoFJWytG7kSoDGHWDi3Qi2ZofGSkOUfM5tf8Y2F7JxQ_BPDmepo.vN9FPfTSAX8YQWVn7KImHwQ8etGnwtjHU1X53LqeOszpdQzJFRz.56Io6ZhKggx5Sw19C8ZeaFSvL07x2DGZU2B1R95&v=2; path=/; domain=in.yahoo.com
Set-Cookie: fpc_s=d=hVjAbR8UWh.3C6IKs4uB5Br.uhIQuGAdy635M1qsR7rU3lCDZexkFtqPmxIo_NNjWWY.HMGPDLNVNsSLY0hfPspxeAFXlfn_NEUJIuprb6yCx0eEKtShbiiZgT5s_9XEwZ_3IOuCz3Z55NDE8.q1VQHOhpQe80T9TgYCROHmruHPAuuaShN2U8ZtUtLlzuc1iz6EwFnXOzwyYiuOeFylk_vK4wMm36ixCDUuAbdJzmzPHymxOaniTOG78kCf7419PRkJjU8zeBrvCSZ3MKJPDRvmOWSTdDoRLcoFJWytG7kSoDGHWDi3Qi2ZofGSkOUfM5tf8Y2F7JxQ_BPDmepo.vN9FPfTSAX8YQWVn7KImHwQ8etGnwtjHU1X53LqeOszpdQzJFRz.56Io6ZhKggx5Sw19C8ZeaFSvL07x2DGZU2B1R95&v=2; path=/; domain=in.yahoo.com
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8
< Age: 0
Age: 0
< Connection: keep-alive
Connection: keep-alive
< Server: YTS/1.20.0
Server: YTS/1.20.0
* no chunk, no close, no size. Assume close to signal end

<
* Closing connection #1
* Closing connection #0
senseexcept-lm:~ hitesha$

The above curl command, `curl --verbose --head --location www.yahoo.com` tells us that curl should be verbose, it should just send a "HEAD" HTTP request instead of GET, and that it should follow any "Location: [...]" responses that it receives from the server (telling it to go to a new location  --302 http redirect). In this case, as we've seen above, curl will re-run the request with the updated location as the input URL. This behavior mimics that of browsers, but it isn't the default curl behavior.









There are some cases when we might need to more closely simulate a browser. If your application handles GET requests and ignores HEAD, you might need to omit '--head' and instead just use '-o /dev/null' to write the downloaded file out to nowhere. You could also just leave this out, which will dump the page source to your STDOUT (effectively the same as '-o -'). You might also need to specify a user agent string to trigger specific site behavior such as a mobile site. Let's try with example.com again. We're going to simulate a request from an iPhone running iOS 5.0 using Safari, with the '--trace-ascii' for full geek mode which will show us even more details that could be useful for optimized mobile content. We'll also use the '--limit-rate' option for curl to slow down the transfer to a crawl (2G cellular GPRS speeds that an average connection might see -- 25kbit/sec):

 curl –trace-ascii – –location -o /dev/null 
–user-agent ‘Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) 
AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 
Safari/7534.48.3′ –limit-rate 25k example.com

No comments:

Post a Comment