Why One Rewrite Rule Works and the Other Fails: Mastering Apache URL Rewrites
This article compares two Apache rewrite configurations, showing why the first correctly captures query parameters while the second does not, and explains the crucial role of the %{QUERY_STRING} variable in successful URL rewriting.
The article presents two Apache mod_rewrite configuration examples and analyzes their behavior.
First configuration (works)
# Enable RewriteEngine
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(/)?$ /wap.php?%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^/(\w+)\.php$ /$1.php?%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\.html$ /wap.php?controller=$1&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\/(\w+)\.html$ /wap.php?controller=$1&action=$2&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\/(\w+)\/( [\w\-\=\&\/a-zA-Z0-9_%]+)\.html$ /wap.php?controller=$1&action=$2&arguments=$3&%{QUERY_STRING} [L]
RewriteRule ^app\/$ /app.php [L]
RewriteRule ^(/)?$ /index.php [L]
RewriteRule ^/(\w+)\.php$ /$1.php [QSA,L]
RewriteRule ^(\w+)\.html$ /index.php?controller=$1 [QSA,L]
RewriteRule ^(\w+)\/(\w+)\.html$ /index.php?controller=$1&action=$2 [QSA,L]
RewriteRule ^(\w+)\/(\w+)\/( [\w\-\=\&\/a-zA-Z0-9_%]+)\.html$ /index.php?controller=$1&action=$2&arguments=$3 [QSA,L]This configuration correctly captures parameters such as p=2 from URLs like http://m.zcyy.dev/auction/index/type/auction.html?p=2, returning p=2 as expected.
Second configuration (fails)
# Enable RewriteEngine
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(/)?$ /wap.php [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^/(\w+)\.php$ /$1.php [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\.html$ /wap.php?controller=$1 [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\/(\w+)\.html$ /wap.php?controller=$1&action=$2 [L]
RewriteCond %{HTTP_HOST} ^m.zcyy.dev [NC]
RewriteRule ^(\w+)\/(\w+)\/( [\w\-\=\&\/a-zA-Z0-9_%]+)\.html$ /wap.php?controller=$1&action=$2&arguments=$3 [L]
RewriteRule ^app\/$ /app.php [L]
RewriteRule ^(/)?$ /index.php [L]
RewriteRule ^/(\w+)\.php$ /$1.php [QSA,L]
RewriteRule ^(\w+)\.html$ /index.php?controller=$1 [QSA,L]
RewriteRule ^(\w+)\/(\w+)\.html$ /index.php?controller=$1&action=$2 [QSA,L]
RewriteRule ^(\w+)\/(\w+)\/( [\w\-\=\&\/a-zA-Z0-9_%]+)\.html$ /index.php?controller=$1&action=$2&arguments=$3 [QSA,L]This version fails to retrieve the query string, so a request like http://m.zcyy.dev/auction/index/type/auction.html?p=2 does not return the p parameter.
The key difference is the omission of %{QUERY_STRING} in the rewrite rules of the second configuration; without it, the original query parameters are discarded, preventing proper parameter extraction.
When the same path is accessed via the domain www.zcyy.dev, the parameter is correctly captured because the rule set for that host includes the %{QUERY_STRING} placeholder.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
