Why is Apache ignoring the rewrite rule

apache-2.2mod-rewriterewrite

I have an old application with URLs of the form…

http://apps.myserver.com/appname/viewItem.akt?p=123

I am replacing this old app with one that has URLs like…

http://appname.myserver.com/item/123

I've tried adding several different rewrite rules to httpd/httpd.conf and restarting the server, but I keep getting 404 errors.

A couple things I've tried…

RewriteEngine on

RewriteRule ^.*viewItem.akt\?p=([0-9]*)$ http://appname.myserver.com/item/$1 [R]

Or…

RewriteRule ^/appname/viewItem.akt\?p=([0-9]*)$ http://appname.myserver.com/item/$1 [R]

The result is always "The requested URL /appname/viewItem.akt was not found on this server", as if the rewrite rule weren't even there.

What I'm getting in the access log is…

[IP address] - - [09/Sep/2011:21:27:37 -0700] "GET /appname/viewItem.akt?p=1018 HTTP/1.0" 404 248

I tested all my regexes in Rubular to make sure they match the incoming request.

What am I missing?


In case it's relevant, the old application was a Java app running under Tomcat, and I had a ProxyPass set up like this…

(I'm showing these lines commented out, as they are when I'm trying to get the rewrite rule to work.)

# ProxyPass /appname http://apps.myserver.com:8080/appname
# ProxyPassReverse /appname http://apps.myserver.com:8080/appname

But that shouldn't matter right? As long as my rewrite rule matches the request it should send it along properly, shouldn't it?

Best Answer

Try this:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^appname/viewItem.akt$ http://appname.myserver.com/item/%1? [R,L]
  • %1 to back reference to Rewrite Condition
  • ? at the end of substitution to remove the rest of query string

But it seems you are mixing up mod_rewrite with mod_proxy. Which Apache version are you running?

Related Topic