QuickAnswer
by

Debugging mod_rewrite and RewriteRule. Displaying matched values for regular expressions.

Debugging mod_rewrite and RewriteRule. Displaying matched values for regular expressions.

If you want to check the behavior of mod_rewrite and RewriteRule, how do you debug them?
I want to know the string that matched the regular expression.

Example of mechanism installation

The following are the test results on localhost.
Please replace the localhost part with https://www.example.com/ or something similar.

Install .htaccess in localhost/sub/.
If you access localhost/sub/123

RewriteRule ^(.*)$ /?$1 [R,END]

You will be redirected to the following
localhost/?123
The URL field will display localhost/?123 and
A string matching ^(. *)$ can be retrieved as query parameters.

Retrieving REQUEST_URI, etc.

RewriteRule ^(.*)$ /?%{REQUEST_URI} [R,END]

localhost/?/sub/123
We can see that the REQUEST_URI is /sub/123.

Multiple simultaneous extraction

RewriteRule ^(.*)$ /?M=$1,RF=%{REQUEST_FILENAME},RU=%{REQUEST_URI} [R,END]

localhost/?M=123,RF=D:/Documents/html/sub/123,RU=/sub/123

Other examples of execution

Writing RewriteRules and their results

RewriteRule ^(.*)$ /?$1 [R,END]
# 123

RewriteRule ^(.*)$ /?%{HTTP_USER_AGENT} [R,END]
# Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0

RewriteRule ^(.*)$ /?%{HTTP_REFERER} [R,END]
#

RewriteRule ^(.*)$ /?%{HTTP_COOKIE} [R,END]
#

RewriteRule ^(.*)$ /?%{HTTP_FORWARDED} [R,END]
#

RewriteRule ^(.*)$ /?%{HTTP_HOST} [R,END]
# localhost

RewriteRule ^(.*)$ /?%{HTTP_PROXY_CONNECTION} [R,END]
#

RewriteRule ^(.*)$ /?%{HTTP_ACCEPT} [R,END]
# text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

RewriteRule ^(.*)$ /?%{REMOTE_ADDR} [R,END]
# 127.0.0.1

RewriteRule ^(.*)$ /?%{REMOTE_HOST} [R,END]
# 127.0.0.1

RewriteRule ^(.*)$ /?%{REMOTE_USER} [R,END]
#

RewriteRule ^(.*)$ /?%{REMOTE_IDENT} [R,END]
#

RewriteRule ^(.*)$ /?%{REQUEST_METHOD} [R,END]
# GET

RewriteRule ^(.*)$ /?%{SCRIPT_FILENAME} [R,END]
# D:/Documents/html/sub/123

RewriteRule ^(.*)$ /?%{PATH_INFO} [R,END]
#

RewriteRule ^(.*)$ /?%{QUERY_STRING} [R,END]
#

RewriteRule ^(.*)$ /?%{AUTH_TYPE} [R,END]
#

RewriteRule ^(.*)$ /?%{DOCUMENT_ROOT} [R,END]
# D:/Documents/html

RewriteRule ^(.*)$ /?%{SERVER_ADMIN} [R,END]
# postmaster@localhost

RewriteRule ^(.*)$ /?%{SERVER_NAME} [R,END]
# localhost

RewriteRule ^(.*)$ /?%{SERVER_ADDR} [R,END]
# 127.0.0.1

RewriteRule ^(.*)$ /?%{SERVER_PORT} [R,END]
# 80

RewriteRule ^(.*)$ /?%{SERVER_PROTOCOL} [R,END]
# HTTP/1.1

RewriteRule ^(.*)$ /?%{SERVER_SOFTWARE} [R,END]
# Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.0.3

RewriteRule ^(.*)$ /?%{TIME_YEAR} [R,END]
# 2021

RewriteRule ^(.*)$ /?%{TIME_MON} [R,END]
# 10

RewriteRule ^(.*)$ /?%{TIME_DAY} [R,END]
# 06

RewriteRule ^(.*)$ /?%{TIME_HOUR} [R,END]
# 14

RewriteRule ^(.*)$ /?%{TIME_MIN} [R,END]
# 47

RewriteRule ^(.*)$ /?%{TIME_SEC} [R,END]
# 45

RewriteRule ^(.*)$ /?%{TIME_WDAY} [R,END]
# 3

RewriteRule ^(.*)$ /?%{TIME} [R,END]
# 20211006144807

RewriteRule ^(.*)$ /?%{API_VERSION} [R,END]
# 20120211:93

RewriteRule ^(.*)$ /?%{THE_REQUEST} [R,END]
# GET%20/%3fGET%2520/%253fGET%252......HTTP/1.1(Abbreviation)

RewriteRule ^(.*)$ /?%{REQUEST_URI} [R,END]
# /sub/123

RewriteRule ^(.*)$ /?%{REQUEST_FILENAME} [R,END]
# D:/Documents/html/sub/123

RewriteRule ^(.*)$ /?%{IS_SUBREQ} [R,END]
# false
CONTENTS
Web Browser