Click to See Complete Forum and Search --> : escaping "?" char in the Rewriterules
walidaly
06-07-2006, 06:16 PM
I'm trying to convert http://mydomain.com/search?anyvar=query into
http://mydomain.com/search.php?q=query
tried this
Rewriterule ^search?.*=(.*)$ search.php?q=$1 [nc]
but it seems as the ? char is misunderstood by apache and I can't escape it with \?
any ideas?
Scleppel
06-09-2006, 04:26 PM
Query strings are matched with the %{QUERY_STRING} variable and RewriteCond:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+=(.+)$
RewriteRule ^search\.php$ search.php?q=%1 [L]
walidaly
06-09-2006, 04:35 PM
I believe that will convert any query not only the one inside search folder http://mydomain.com/search?anyvar=query
the "search" folder doesn't exist so I'm editing the root .htaccess directly
Scleppel
06-09-2006, 04:52 PM
Sorry, the mistake is i wrote "search\.php" instead of "search/?":
RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+=(.+)$
RewriteRule ^search/?$ /search.php?q=%1 [L]
It will first match "/search", then it will check if it has a query string that is "anything=query" and if it has, send it to "search.php?q=query".
walidaly
06-10-2006, 11:20 PM
worked great! Thanks!
nightster
06-13-2006, 03:51 PM
Hey all,
I need to do a similar (but different) thing....
I have several domains all pointing to one server.
I need to redirect something like this:
www.domain.com/test
To something like this:
www.domain.com/domain/main.php?p=1
This is what I have so far in the root level .htaccess file:
RewriteRule ^/test$ http://www.domain.com/domain/main.php\?p=1
It doesn't work :(
If I remove the ?p=1 bit from the end, it works - but I need to pass that parameter to the php file.
I am guessing it's an issue with the question mark. I have tried escaping it with a \, but that didn't work.
Any ideas?!? This is pretty urgent, so if you can help I would really appreciate it.
Thanks again,
Rob.
Scleppel
06-13-2006, 04:31 PM
In the output URL (not the pattern) you don't need to escape the "?".
The problem is most likely that you have a "/" before "test", you only need that in *.conf files, try this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test/?$ http://www.domain.com/domain/main.php?p=1
nightster
06-13-2006, 04:38 PM
Thanks, but still no luck.
This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test/?$ http://www.domain.com/domain/main.php?p=1
Any other suggestions please? Is it anything to do with the fact that I have mutliple domains pointing to one server?
Thanks again!
Scleppel
06-13-2006, 04:50 PM
First, have you ever use mod_rewrite before? Do you know for sure that you are able to use mod_rewrite?
Try putting this into a .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^google$ http://www.google.com/ [R,L]
Then go to "yourdomain.com/google", if you are redirected to Google, mod_rewrite works. If it doesn't you should ask your hosting support poeple.
Is it anything to do with the fact that I have mutliple domains pointing to one server?
Are the domains pointing to one server, or one document root (the same files)?
What exactly are you trying to do? Is it supposed to be dynamic, eg.
domainone.com/test/ --> /domainone/main.php?p=1
domaintwo.com/test/ --> /domaintwo/main.php?p=1?
Is the user supposed to see the redirect?
nightster
06-13-2006, 04:56 PM
Thanks again for your reply.
This:
RewriteRule ^google$ http://www.google.com/ [R,L]
doesn't work
But if I change it to this:
RedirectMatch ^/google$ http://www.google.com/ [R,L]
it does!
Is that normal? Can RewriteRule work and RedirectMatch not?
Thanks!
Scleppel
06-13-2006, 05:09 PM
RedircetMatch (http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch) is part of mod_alias (http://httpd.apache.org/docs/2.0/mod/mod_alias.html) not mod_rewrite (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html).
Is that normal? Can RewriteRule work and RedirectMatch not?
They are different modules, one can be enabled and the other can be disabled.
Depending on what you are trying to do, RedirectMatch might work. Maybe this:
RedirectMatch ^/test/?$ http://www.domain.com/domain/main.php?p=1
But if you want to do anything complex, or have the user not see the redirect, you'll probably need mod_rewrite. Then you'll have to see if you can have it enabled on your server.
nightster
06-13-2006, 05:14 PM
No, that doesn't work. Damn!
I guess I will have to have modrewrite enabled on my server.
Thanks for all your help!
Cheers,
Rob.
nightster
06-13-2006, 06:36 PM
Solved! Turns out I was putting the .htaccess file in the VERY root directory, instead of in the public_html directory where it belongs.
Thanks so much for all your help!