URL pattern for (question mark )? and & (ampercent)

Discuss practical ways rearrange URLs using mod_rewrite.

Postby richardk » Wed Oct 01, 2008 11:11 am

On what server I'll need to have Apache 2.2, on one where .htaccess is located (opensearch.domain.com) or on destination one (domain.com)?

On the opensearch.example.com server. Then you can try the B flag
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule wrote:'B' (escape backreferences)

Apache has to unescape URLs before mapping them, so backreferences will be unescaped at the time they are applied. Using the B flag, non-alphanumeric characters in backreferences will be escaped. For example, consider the rule:

RewriteRule ^(.*)$ index.php?show=$1

This will map /C++ to index.php?show=/C++. But it will also map /C%2b%2b to index.php?show=/C++, because the %2b has been unescaped. With the B flag, it will instead map to index.php?show=/C%2b%2b.

This escaping is particularly necessary in a proxy situation, when the backend may break if presented with an unescaped URL.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby D.i M.e » Sun Oct 12, 2008 10:58 am

I tried opensearch.example.com on VPS running Apache 2.29 and used your first code but again it doesn't redirect to correct URL.

I didn't succeed to create some useful code with that B flag, since I'm not experienced in coding this, but if I understood correctly documentation, it map only if it is in query, but I don't have those unescapes in query.

So, what to do?
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Postby richardk » Mon Oct 13, 2008 9:51 am

Did you try
Code: Select all
ptions +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
  RewriteRule ^opensearch/?$ http://example.com/search/?cx=017117527339218713940\%3Akjicmwrqgu4&cof=FORID\%3A11&ie=UTF-8&sa=\%D0\%9F\%D0\%BE\%D1\%82\%D1\%80\%D0\%B0\%D0\%B6\%D0\%B8 [B,P,L]
</IfModule>

?

Is there any way of finding out what is wrong with the URL when it gets to the search script?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby D.i M.e » Mon Oct 13, 2008 10:34 am

Nope, this code is not doing thing.

I'll try to explain you again, maybe you didn't understood my first try. For this example I'll use your first code

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
RewriteRule ^opensearch/?$ http://example.com//search/?cx=017117527339218713940\%3Akjicmwrqgu4&cof=FORID\%3A11&ie=UTF-8&sa=%D0\%9F%D0%BE%D1\%82%D1\%80%D0%B0%D0%B6%D0%B8 [QSA,L]


on Apache 2.2.9.

Let's say I want to search for "test. I should have URL

Code: Select all
http://opensearch.example.com/opensearch?q=test


that should redirect me. It redirects me to this page

Code: Select all
http://example.com/search/?cx=017117527339218713940%253Akjicmwrqgu4&cof=FORID%253A11&ie=UTF-8&sa=%25D0%259F%25D0%25BE%25D1%2582%25D1%2580%25D0%25B0%25D0%25B6%25D0%25B8&q=test


but correct URL should be

Code: Select all
http://example.com/search/?cx=017117527339218713940%3Akjicmwrqgu4&cof=FORID%3A11&ie=UTF-8&sa=%D0%9F%D0%BE%D1%82%D1%80%D0%B0%D0%B6%D0%B8q=test
.

As you can see, there are differences between this two URLs, e.g.

Code: Select all
&cof=FORID%253A11


from redirected page should be

Code: Select all
&cof=FORID%3A11


So, mistake is in this part of code (should be highlighted with tag for bold, but bold is not possible in code):

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
RewriteRule ^opensearch/?$ [b]http://example.com//search/?cx=017117527339218713940\%3Akjicmwrqgu4&cof=FORID\%3A11&ie=UTF-8&sa=%D0\%9F%D0%BE%D1\%82%D1\%80%D0%B0%D0%B6%D0%B8 [/b][QSA,L]


I hope you understand now. I appreciate you help.
Thanks
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Postby richardk » Tue Oct 14, 2008 9:38 am

Try
Code: Select all
Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
  RewriteRule ^opensearch/?$ http://example.com/search/?cx=017117527339218713940\%3Akjicmwrqgu4&cof=FORID\%3A11&ie=UTF-8&sa=\%D0\%9F\%D0\%BE\%D1\%82\%D1\%80\%D0\%B0\%D0\%B6\%D0\%B8 [B,NE,P,L]
</IfModule>


If that doesn't work, i can't think of a way to stop the double encoding. You could send the requests through PHP or possibly set up a RewriteMap (requires httpd.conf file access) to clean up the bad encoding.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 18 guests

cron