Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite experts, please help

mod rewrite problem

         

colombo

7:37 am on May 3, 2008 (gmt 0)

10+ Year Member



i need to do the following rewrite:

mydomain.com/dir/var1.html?var2=x&var3=y

to

mydomain.com/test.php?var1=var1&var2=var2&var3=var3

i try this rule:

RewriteRule ^dir/(.*)\.html?var2=(.*)&var3=(.*)$ test.php?var1=$1&var2=$2&var3=$3

the problem is with the "?" sign after the .HTML (RewriteRule ^dir/(.*)\.html?)

if i replace the "?" sign with any other sign its work.

the problem is that i have to use the "?" sign.

if you know what the code should look like please help.

thank you.

jdMorgan

12:47 pm on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this recent thread [webmasterworld.com] to get started.

Jim

colombo

3:10 pm on May 3, 2008 (gmt 0)

10+ Year Member



Hi Jim,
i saw some related posts about this issue but i didn't understand them.

can you give me another example to clear it up or tell me what should my rewrite rule look like in my case.

thanks for your help, trust me Jim i'm not lazy i really don't know what to do with the information that you gave me.

thanks in advance... :)

g1smd

4:51 pm on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



QueryString is NOT a part of the URL.

It is therefore catered for in a separate part of your rule.

colombo

5:18 pm on May 3, 2008 (gmt 0)

10+ Year Member



thanks for your help but i don't know what to do with this information.

can you give some examples or direct me to site where i can find more information about my problem?

thanks

jdMorgan

5:33 pm on May 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We don't mean to be unhelpful, but the fact is that messing with mod_rewrite without learning about it is quite dangerous to the health of your site. Therefore, as described in our Forum Charter, we ask that you read the mod_rewrite documentation, and make an effort to write the code on your own. Otherwise, you're playing with a hand-grenade, because mod_rewrite affects your server configuration. One typo or character out of place can take your site off-line or destroy your search engine rankings.

You will also find that most hosting companies provide no support for mod_rewrite; They don't want to be responsible for messing up your server.

We welcome specific questions here, but we cannot write your code for you. You are the one who will have to thoroughly test it and maintain it over time; It would be best to learn all you can about it. Otherwise, you might prefer to hire someone to configure your server and look after it.

There are also several very useful links in our Forum Charter to help you get started, including a link to the Apache mod_rewrite documentation. In addition, there are many threads on this subject available by using Site Search, and some tutorials and primers in our Forum Library. All of these resources are available in the links at the top of every forum page on WebmasterWorld.

When reviewing the mod_rewrite documentation, pay particular attention to the %{QUERY_STRING} variable, as tested by the RewriteCond directive.

Jim

colombo

9:45 pm on May 3, 2008 (gmt 0)

10+ Year Member



i wrote all the mod rewrite by my self .

i just don't know what to do with the "?" sign.

all i'm asking is to get some examples similar to my problem.

you can give me the line/rule that will solve my problem i will take the responsibility for using it.

i kind of lost , i'm looking for answers for few days now and its really important to me because i'm losing money.

so please, if you know the solution for my problem please tell it to me.

jdMorgan

3:18 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING} ^var2=[^&]+&var3=[^&]+
RewriteRule ^dir/([^.]+)\.html$ test.php?var1=$1 [QSA,L]

[QSA] appends the new query name/value pairs to the existing query string.

If the variable order in the rewritten query string is critical, then use:


RewriteCond %{QUERY_STRING} ^var2=([^&]+)&var3=([^&]+)
RewriteRule ^dir/([^.]+)\.html$ test.php?var1=$1&var2=%1&var3=%2 [L]

If this isn't exactly what you need, it should be close enough for you to get it working, with the assistance of all of the resources cited above.

Jim

colombo

5:37 pm on May 4, 2008 (gmt 0)

10+ Year Member



Hi

i just used this rule

RewriteRule ^dir/([^/]+)\.html$ /test.php?var1=$1 [QSA,L]

and it did the job.

thanks for the help.

jdMorgan

6:14 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The pattern is incorrect -- or at least inefficient. Use:

RewriteRule ^dir/([^.]+)\.html$ /test.php?var1=$1 [QSA,L]

-or-

RewriteRule ^dir/([^/.]+)\.html$ /test.php?var1=$1 [QSA,L]

depending on your requirements. The first version allows subdirectories below "/dir" and will include their path in $1. The second version will not accept a URL that has a subdirectory below "/dir" in the path. But in either case, you probably want to stop matching the initial path-part when you find a period for best performance.

Jim