Htaccess conditional rewrite

hi guys

first time post. been trying to work out this problem now all day.

I am first of all rewriting URLs that come in from a source that I cannot change:

RedirectMatch permanent ^/$

Basically, I do not want to redirect if url is like this:{test}&id2={test2}

I simply want to maintain that url. I need to keep the same if the the query_string is as above else redirect. I tried something like this but I know that it is not correct:

RewriteCond %{QUERY_STRING} ^id={test}&id2={test2}.*
RedirectMatch permanent ^/$

RedirectMatch permanent ^/$

If anyone can help this would be a great help!

Mr I,

Interesting! You’re mixing apples (mod_alias) with oranges (mod_rewrite)! It just won’t work that way. If you want to learn about mod_rewrite (including code samples), look at the tutorial Article linked in my signature.

Just what, SPECIFICALLY, are you looking to match and redirect, NOTHING (an empty {REQUEST_URI})? WHAT is supposed to be done with the query string?

IMHO, the process of generating mod_rewrite (okay, ANY) code is to create a specification. You’ve shown a couple of examples but they seem to miss the point so an explanation of what you REALLY want to do would be most helpful.

Redirect to a subdirectory if no file specified unless there is a query string?

Another point: If the redirection is hidden (internal and NOT R=301), why make Apache go to the extra effort of determining which DirectoryIndex file to serve.



Hi David, thanks for the response.

I think a pseudo explanation will help here. I really don’t understand the best way to tackle my problem, here are the two scenarios:

  1. get all incoming urls to* and redirect to*
  2. if incoming URL is of format that contains ?key={pair}&key2={pair2} do not follow the rule above, simple leave as is.

In the second example above there are literally incoming urls that have those braces {}.

I hope that makes sense from an objective point of view. I’m reading the tutorials in your signature, alas, I have been researching all day this problem.

Again, thanks again for the response.

UPDATE/EDIT: @David, link to is broken.
UPDATE/EDIT: Seems most links are broken on your website.

Mr I,

Thanks for the note about my site. I got the failure message, too, and uploaded the .htaccess (again) and that cleared the problem. I have no idea what happened to do that!

Okay, that’s closer to a specification:

  1. get all incoming urls to* and redirect to*
  2. if incoming URL is of format that contains ?key={pair}&key2={pair2} do not follow the rule above, simple leave as is.
RewriteEngine on

# look for key and key2 in the query string then skip the redirection
RewriteCond %{QUERY_STRING} key=
RewriteCond %{QUERY_STRING} key2=
RewriteRule .? - [PT,S=1]

# redirect EVERYTHING to STATIC_FOLDER (if not to STATIC_FOLDER, i.e., no loops)

The S=1 is SKIP the next (one) RewriteRule and will only happen if key and key2 are keys in the query string. The second one ensures that the {REQUEST_URI} is not to your static folder then, if not, redirects to the static folder.



Thanks David, glad you uploaded the htaccess file and sorted the site, great information on there.

As for this line:

RewriteCond %{QUERY_STRING} key=

Would it be possible to do this:

RewriteCond %{QUERY_STRING} key={pair}

assuming i have a url that looks like this:{pair}

^ And yes, the url is literally like this.

Mr I,

Yes, several years of member questions (and code) in that tutorial!

As for your RewriteCond, do you NEED to match the value of the key(s)? If not, why include them? Of course, you can attempt to match key={pair} but I wasn’t sure that the curly braces are permitted in a URI (okay, it’s in the query string but I think it’s the same thing - I’ll let you check Tim Berners-Lee’s [url=]Uniform Resource Identifiers (URI): Generic Syntax



Ah yes, I’ll have to escape these. So with this is mind to represent something passed like “key={pair}”, I think this would be represented by:


Other characters are excluded because gateways and other transport
agents are known to sometimes modify such characters, or they are
used as delimiters.

unwise = “{” | “}” | “|” | “\” | “^” | “[” | “]” | “`”

Data corresponding to excluded characters must be escaped in order to
be properly represented within a URI.

I’ll get cracking on this today, once again, I appreciate your knowledge on this. Many thanks.

Mr I,

No problem! Most people (myself included years ago - and I have to check that link every time something weird is requested by a client) are unaware that ANY character may be “illegal” in a URI (and wonder why a space shows up as %20 :lol: ).