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

Discuss practical ways rearrange URLs using mod_rewrite.

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

Postby sam » Sat Aug 23, 2008 10:41 pm

Hi,

I have requirement where in

www.abc.com/languagecode/books/?langId=12&Id=12


url should redirect to

/nameofcontextpath/viewBook.form ?langId=12&Id=12



Also

I have requirement where in

www.abc.com/languagecode/books/travelgu ... d=12&Id=12


url should redirect to

/nameofcontextpath/viewBook.form?name=travelguide&langId=12&Id=12

Can someone tel me pattern for this?



I have tried


RewriteRule ^/([A-Za-z]{2})/(books)/([a-zA-Z[-a-zA-Z]+]*)/?langId=(\d+)/&Id=(\d+) /nameofcontextpath/viewBook.form?name=$3&langId=$4&Id=$5 [PT,L]

However it does not work
sam
 
Posts: 5
Joined: Sat Aug 23, 2008 3:34 am

Postby richardk » Sun Aug 24, 2008 2:26 pm

You can use the QSA flag to pass on the query string.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^/[a-zA-Z]{2}/books/([-a-zA-Z]+)/?$ /nameofcontextpath/viewBook.form?name=$1 [QSA,PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sam » Thu Aug 28, 2008 1:52 am

Thanks.
It works.
sam
 
Posts: 5
Joined: Sat Aug 23, 2008 3:34 am

Postby D.i M.e » Wed Sep 17, 2008 11:06 am

I have similar question. I tried to solve it with solution from here but didn't have success.

I want to make OpenSearch for use in search bars in Firefox and IE so that users can search my site directly from their browsers.

Why I'm asking here is this: I want that URL used in that search bars are redirected to actual URL of search. This is because I currently use Google CSE for search but later I'll maybe use some internal search or something else, so if I made this change users won't see it because they'll be redirected to that new search. If I give them current search URL they'll need to update their search bar later or I'll need to make complicated redirection.

So what I want is this:
this URL
http://domain.com/opensearch/?q=keyword
to be redirected to
http://domain.com/search/?cx=0171175273 ... &q=keyword

So only variable that should be taken is "keyword" (everything after q=).

Thanks in advance
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Postby richardk » Fri Sep 19, 2008 7:15 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
RewriteRule ^opensearch/?$ /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]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby D.i M.e » Fri Sep 19, 2008 1:45 pm

Thanks, Richard, for help but this doesn't redirect, I get 404 error.

Maybe I should give you notice that I have already one .htaccess in root with following code:

Code: Select all
# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]



</IfModule>

# END WordPress


Maybe it is problem with that code?
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Postby richardk » Fri Sep 19, 2008 1:51 pm

Try
Code: Select all
Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{QUERY_STRING} ^(.*&)?q= [NC]
  RewriteRule ^opensearch/?$ /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]

  # BEGIN WordPress
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule . /index.php [L]
  # END WordPress
</IfModule>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby D.i M.e » Mon Sep 22, 2008 1:16 pm

Richard, thank you very much for you help, I really appreciate it.

Unfortunately, this is not solving problem, partly. I say partly, because your code is working on non-WordPress pages. Let's explain.
I tested this first on different site and then again on WordPress installation by creating empty folder called search. In both cases redirection worked. That means that server will redirect it only if there is page or folder called "search".

What I wanted in my case is to redirect it to my page on WordPress with URL domain.com/search which doesn't exists as page or folder, but it is dynamically created by WordPress (you understand thing). If I normally open that search with all that parameters via referral link or directly in address bar or via search form it works fine, but it seems that redirection doesn't works because server doesn't see that page "search" and because of that it gives 404 error.

How I actually succeed to redirect is from another domain or subdomain, by entering URL of site like in example below.

Code: Select all
RewriteRule ^opensearch/?$ http://domain.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]

(of course there was also rest of the code, but I pasted changed one)

I tried this on that WordPress installation but it didn't work, again I received 404 error.

So there are two possible solutions: to find way to redirect it as I intended or to create special subdomain just for it (let's say opensearch.domain.com) from which queries will be automatically redirected to right place.

But before on both solutions your decoding of parameters must be changed: when I succeed to redirect from other subdomain, instead of first URL I got second one and Google CSE won't work with it, it only fill search form on that page so user must again click on it.

Code: Select all
http://domain.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&q=keyword

http://domain.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=keyword


What is your suggestion about this?

Thanks again
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Postby richardk » Tue Sep 23, 2008 12:06 pm

The problem is Wordpress, it is using the REQUEST_URI (which is /opensearch) so it's ignoring what mod_rewrite has done.

But before on both solutions your decoding of parameters must be changed: when I succeed to redirect from other subdomain, instead of first URL I got second one and Google CSE won't work with it, it only fill search form on that page so user must again click on it.

Unless you have Apache2.2 you have to fix that with PHP.

Mod_rewrite can't fix this. You need to add some PHP to set the correct variables in your index.php.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby D.i M.e » Tue Sep 30, 2008 12:27 pm

richardk wrote:The problem is Wordpress, it is using the REQUEST_URI (which is /opensearch) so it's ignoring what mod_rewrite has done.

But before on both solutions your decoding of parameters must be changed: when I succeed to redirect from other subdomain, instead of first URL I got second one and Google CSE won't work with it, it only fill search form on that page so user must again click on it.

Unless you have Apache2.2 you have to fix that with PHP.

Mod_rewrite can't fix this. You need to add some PHP to set the correct variables in your index.php.


OK then, I'll use solution with subdomain.

richardk wrote:
But before on both solutions your decoding of parameters must be changed: when I succeed to redirect from other subdomain, instead of first URL I got second one and Google CSE won't work with it, it only fill search form on that page so user must again click on it.

Unless you have Apache2.2 you have to fix that with PHP.

Mod_rewrite can't fix this. You need to add some PHP to set the correct variables in your index.php.


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)?
D.i M.e
 
Posts: 11
Joined: Tue Apr 24, 2007 11:56 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

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

cron