Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite: check if there is '?' or '=' in URL

         

godfrank

1:49 am on Jul 10, 2005 (gmt 0)

10+ Year Member



I'd like to have mod_rewrite to detect if there is either '?' or '=' in the URL in order to change value of my requested URL.

ie:
I want to add this code to every URL:
?lang=en
I need mod_rewrite to detect if there is already a '?' in order to change '?lang=en' to '&lang=en' ie: index.php?info=bob&lang=en

For now, what I have done is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*?.*
RewriteRule ^test/(.*)$ /$1 [NC]

It works as long as I comment out the third line (RewriteCond)... and I am trying with a URL ending with?var=value... I am still at the point where I just want to see if it detects a '?' in the URL.

How can I do this? Thanks a lot folks!

jdMorgan

2:32 am on Jul 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



godfrank,

Welcome to WebmasterWorld!

Normally, you don't need to do this; Just use the [QSA] flag to append new query name/value pairs.

In order to test for "lang=" all you need to do is to use RewriteCond and test the current %{QUERY_STRING} to see if it contains that string.

Note that query strings do not appear in %{REQUEST_URI} and are not visible to RewriteRule because query strings are not part of a URL, they are data appended to a URL to be passed to the resource at that URL. For this reason, they are handled separately.

Also, I should note that you cannot actually 'see' the "?" in either case; It serves as a separator between URLs and query strings, and is not part of either.

Jim

godfrank

5:03 pm on Jul 10, 2005 (gmt 0)

10+ Year Member



It works!

Many thanks!