Modified: 07th Jul 14 |

Apache/httpd Redirect/RedirectMatch problem

Apache query redirect with special characters

Problem: Redirecting pages/requests with special characters will not work with apache.

I tried to do something like this:

# Moved
Redirect 301 /?p=1 http://zuu.dk/page1
RedirectMatch 301 ^/$ http://zuu.dk

# Gone (everything else)
Redirect 410 http://zuu.dk

But Apache just refuses to take question mark and equal sign into account. I even tried quoting it, and writing the url encoded counterpart.

Reason: Apparently, the rewrite rule only considers the path component, not the query component. the cuery component is the part of the URL that begins with "?".

Solution: Use mod_rewrite, it can send proper response codes too!
The following directives ended up working exactly as i wanted.

rewriteengine on

# articles are moved
RewriteCond %{query_string} ^p=7$
RewriteRule ^/$ http://zuu.dk/index.php?page=structured-synchronization [R=301]

# front page is movedRewriteCond %{query_string} ^$
RewriteRule ^/$ http://zuu.dk/ [R=301]

# everything else is gone
RewriteRule ^/ http://zuu.dk/ [G]

As can be seen, the mod_rewrite engine can be configured with what response codes to send, just like the redirect directives. The [G] means "Gone" (410), while [R=301] means "Permanently Moved" (301).