Forum Moderators: phranque

Message Too Old, No Replies

Help with a rewrite rule?

         

oldfart

7:16 pm on Sep 13, 2010 (gmt 0)

10+ Year Member



Hello, I was hoping that somone here can help me with a rewrite rule.

here is what I am trying to do...

here is a url generated by the search component of a joomla website
http://examplesite.com/searchterm.html?ordering=&searchphrase=all


what I would like to do is anytime &searchphrase=all
is in the url &Itemid=192 is appended to the URL like this...

http://examplesite.com/searchterm.html?ordering=&searchphrase=all&Itemid=192


Thanks

jdMorgan

9:54 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The part about what you'd like to do seems to be missing.

Also, please post your best-effort code as a basis for discussion, so we have some idea of where to start.

Note that this concurrent thread [webmasterworld.com] contains information that may be useful in your case, depending on your goals.

Thanks,
Jim

oldfart

10:22 pm on Sep 13, 2010 (gmt 0)

10+ Year Member



I thought I spelled it out very well, but I will go in more depth.

I have a joomla website. Joomla is a CMS and dynamically generates urls for content. In joomla you can install 3rd party extensions. These extensions have their own url structure as well.

Our search plugin generates urls that look like this /searchterm.html?ordering=&searchphrase=all

the first part (searchterm.html) is the search query entered into the search box.

the end part (&searchphrase=all) is the default url based on search ordering. In this case it is "show all", but it can also be "alphabetical, oldest first, most recent, etc.)

So what I want to do is re-write anytime there is
&searchphrase=all 

in the URL

I want to keep the URL exactly the same, but I want to add
?Itemid=192
to the end of the URL

So the starting URL is
http://examplesite.com/searchterm.html?ordering=&searchphrase=all


and the rewritten result is
http://examplesite.com/searchterm.html?ordering=&searchphrase=all&Itemid=192


I could go into why i need to append this to the end of the URL but it's long and complicated and I am sure you don't care.

Does this make sense?

as for my best effort code, I dont really have one. I only know the very basics of htaccess rules. I can set 301 redirects, and all of the more advanced stuff I just copy and paste the examples I find on blogs and websites.

I tried to look into it but all of the guides are written by server geniuses that expect you to have a much higher skill level than I have.

Your help is much appreciated.

jdMorgan

11:10 pm on Sep 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming that you already have other working rewriterule in the file, then the following should do that:

RewriteCond %{QUERY_STRING} !^([^&]*&)*Itemid=
RewriteCond %{QUERY_STRING} ^([^&]*&)*searchphrase=all(&.*)?$
RewriteRule ^searchterm\.html$ http://www.example.com/searchterm.html?Itemid=192 [QSA,R=301,L]

The first RewriteCond prevents recursion -- an 'infinite' redirection loop.

Put this rule after your more-specific external redirects, and before any internal rewrites.

Do not mix the use of mod_alias directives such as "Redirect" and "RedirectMatch" with mod_rewrite "RewriteRule" directives. Doing so makes it impossible to control the order of directive execution unless you have server config access and can control the loadmodule order.

More info on these subjects here in our Library: [webmasterworld.com...]

Jim

oldfart

10:36 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



Ok, we are on to something here...
Thanks for your help so far.

The problem I am still having is that the searchterm part of the URL has to be a wildcard, it is literally whatever searchterm someone enters into the box. Sorry if I didnt explain that clearly.

So rightnow the rewrite rule only works if you type "searchterm" into the search box.

To further illustrate the example
http://examplesite.com/gorillas.html?ordering=&searchphrase=all 

would be the URL if I searched for gorillas.
and we want it to rewrite to
http://examplesite.com/gorillas.html?ordering=&searchphrase=all&Itemid=192 


http://examplesite.com/rabbits.html?ordering=&searchphrase=all 

would be the URL if I searched for rabbits.
and we want it to rewrite to
http://examplesite.com/rabbits.html?ordering=&searchphrase=all&Itemid=192 


So basically I need to rewrite rule to keep the searchterm the same as they entered.

Thanks!

g1smd

10:59 pm on Sep 15, 2010 (gmt 0)

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



it is literally whatever searchterm someone enters into the box

You will need to "clean up" what was entered, removing punctuation and other unwanted characters. You'll also need to escape spaces and a whole bunch of other things.

oldfart

11:13 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



g1smd I dont get what you are saying?

The search component strips special characters from the url itself. if the search term is multiple words it hyphenates in the urls.

so if the searchterm was "chickens rabbits fox"

The url would be written thusly.
http://examplesite.com/chickens-rabbit-fox.html?ordering=&searchphrase=all


My issue is the url rewrite has to basically ignore and pass on whatever the searchterm is, but rewrite the end of the URL with Itemid=192 added. I figured the easiest way to do this is look for the &searchphrase=all part of the url and rewrite when it finds it.

Does this make sense?

jdMorgan

3:25 pm on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Match the "searchterm" portion of the requested URL, and then back-reference it in the substitution:

RewriteCond %{QUERY_STRING} !^([^&]*&)*Itemid=
RewriteCond %{QUERY_STRING} ^([^&]*&)*searchphrase=all(&.*)?$
RewriteRule ^(.+)\.html$ http://www.example.com/$1.html?Itemid=192 [QSA,R=301,L]

Please use the resources cited in our Apache Forum Charter to look up the directives on each line, and go character-by-character when necessary to understand *exactly* what this code does. Only then will you have a chance to be successful using mod_rewrite... It is not at all a "cut-and-paste" proposition.

Jim