I'm having trouble with mod_rewrite. There's a broken external link to my site that looks something like this:


I'm trying to create a mod_rewrite rule to fix it (delete the last slash and everything after it). I've tried various things like:

RewriteCond %{QUERY_STRING} myparam=AB/ [NC]
RewriteRule (myparam=AB.*) myparam=AB [L,R]

I have this rule at the top of the rule list, but I get a "Forbidden you don't have access to /foo/page on this server" error. If I delete the last "?" and everything after that from the URL, then the handler for this URL has a parameter of myparam with a value "AB/some-garbage.asp".

I've also tried REQUEST_URI and THE_REQUEST but nothing seems to rewrite this URL. Help please?

1 Answer 1


Try something like this:

RewriteCond %{QUERY_STRING} ^(myparam=AB)/ [NC]
RewriteRule ^(/foo/page)$ $1?%1 [L]

The parts inside parentheses () can be more general regular expressions, should you need to match a wider variety of query strings and URIs.


The RewriteRule above assumes it's not being used in a .htaccess file, but rather in an apache server .conf file (such as httpd.conf).

If your rewrite rule is in an .htaccess file, you need to omit the leading directory part of the URI from the RewriteRule's match pattern. For example, if your .htaccess file is in the foo directory on the server, then your RewriteRule should look like this:

RewriteRule ^(page)$ $1?%1 [L]

(Note how this command is the same as the one above, but with the /foo/ prefix removed from the match pattern.)

  • Thanks, but still doesn't work. The rewrite condition doesn't seem to match the "/".
    – runrig
    Nov 10, 2010 at 16:57
  • I have updated my answer. Nov 10, 2010 at 19:22
  • The only .htaccess is in the html root directory. so my rule is ^(foo/page)$, but $1 in the substitution is replaced by '/var/www/html/foo/page', so I physically made the substitution '/page/foo?%1' which works for now. And Thanks!
    – runrig
    Nov 10, 2010 at 21:05
  • Found out that in order to have $1 not include '/var/www/html' I need the directive 'RewriteBase /' right after the 'RewriteEngine On' directive, and then I can use your answer. Very cool!
    – runrig
    Nov 11, 2010 at 16:33
  • So my final solution is 'RewriteCond (myparam=\w+)/' and 'RewriteRule ^(.*)$ $1?%1' all with a 'RewriteBase /' after the 'RewriteEngine On'
    – runrig
    Nov 11, 2010 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .