Forum Moderators: phranque

Message Too Old, No Replies

url redirect problem for sphider search script

url redirect for sphider

         

Mahabub

7:39 am on Dec 7, 2008 (gmt 0)

10+ Year Member



Hello, I've in problem with the below url redirect code. Though I am not good at htaccess programming, I did find any problem on that but it didn't work. Please help me for the issue.

#sphider rewrite
RewriteEngine on

RewriteCond %{QUERY_STRING} ^query=(.*)\&type=(.*)\&results\=(.*)\&search=1$
RewriteRule ^search\.php$ /search/%2/%3/%1\.html? [R=301,L]

#Did you mean fix
RewriteCond %{QUERY_STRING} ^query=(.*)\&search=1$
RewriteRule ^search/(and¦or¦phrase)/[0-9]{2}/search\.php$ /search/and/10/%1\.html? [R=301,L]

#More results fix
RewriteCond %{QUERY_STRING} ^query=(.*)\&search=1\&results=(.*)&domain=(.*)
RewriteRule ^search/(and¦or¦phrase)/[0-9]{2}/search\.php$ /domain/%2/%3/%1\.html? [R=301,L]
RewriteRule ^domain/(.*)/(.*)/(.*)\.html /search\.php\?query=$3&results=$1&domain=$2&search=1&do=1 [L]

#Redirect main search
RewriteRule ^search/(.*)/(.*)/(.*)\.html /search\.php\?query=$3\&type=$1&results=$2&search=1&do=1 [L]

#Redirect sphider's logo
RewriteRule ^search/(and¦or¦phrase)/[0-9]{2}/(.*)\.png$ htttp://www.throughowleyes.com/$2.png [L]
RewriteRule ^domain/[0-9]{2}/(.*)/(.*)\.png$ htttp://www.throughowleyes.com/$2.png [L]

#Redirect the complete Suggest framework
RewriteRule ^search/(and¦or¦phrase)/[0-9]{2}/include/(.*)$ htttp://www.throughowleyes.com/include/$2 [L]
RewriteRule ^domain/[0-9]{2}/(.*)/include/(.*)$ htttp://www.throughowleyes.com/include/$2 [L]

#Redirect css files
RewriteRule ^search/(and¦or¦phrase)/[0-9]{2}/(.*)\.css$ htttp://www.throughowleyes.com/$2.css [L]
RewriteRule ^domain/[0-9]{2}/(.*)/templates/(.*)\.css$ htttp://www.throughowleyes.com/templates/$2.css [L]

how would i test that my hosting support and¦or¦phrase or not?

Thanks
Mahabub

[edited by: Mahabub at 7:44 am (utc) on Dec. 7, 2008]

g1smd

9:14 am on Dec 7, 2008 (gmt 0)

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



Most of your
(.*)
patterns need to be replaced with something like
([^/]+)
or
([^.]+)
because using
(.*)
is usually very very inefficient.

The rules that are 301 redirects (first three) need to state http and the full domain name in the target URL, otherwise you might run into duplicate content issues.

The rest of the rules say "redirect" in the comments, but each of those is actually a "rewrite" which is something different. Change the comment to say "rewrite".

jdMorgan

5:42 pm on Dec 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As gismd points out above, your regex patterns are extremely inefficient. I suggest using negative-match patterns to implement a "match until you find a specific character" approach. This will execute dozens or hundreds of times faster than code using ".*", because ".*" is 'greedy' and causes the matching engine to make many, many more matching attempts than a straight-left-to-right match using negative-match patterns. Do a search for "greedy regular expressions" for more information.

You may also have a major problem in that you are externally redirecting dynamic URLs to static URLs, and then internally rewriting those static URLs to dynamic filepaths. This causes an 'infinite loop'. The solution is to check to be sure that the client (browser or robot) directly requested the dynamic URL before redirecting it to the static URL, by testing THE_REQUEST in a RewriteCond as shown. This prevents a dynamic URL resulting from an internal rewrite from being redirected back to its static form, rewritten back to dynamic, redirected again, rewritten again, etc.

Example:


# More results fix
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /search(or¦phrase)/[0-9]{2}/search\.php\?query=([^&]+)&search=1&results=([^&]+)&domain=([^&]+)
RewriteRule ^search/(or¦phrase)/[0-9]{2}/search\.php$ /domain/%3/%4/%2\.html? [R=301,L]
#
RewriteRule ^domain/([^/]+)/([^/]+)/([^.]+)\.html /search\.php\?query=$3&results=$1&domain=$2&search=1&do=1 [L]

Note that each negative-match pattern shown above will stop matching when it finds the next delimiter character -- an "&" in query strings, or "/" and "." in URL-paths. I also used "+" as the quantifier, so that no match will occur if the value is blank. This is almost certainly correct in URL-paths, but you need to decide if you want the rules to accept blank values in the query string; If you do, then change the appropriate quantifier(s) back to "*".

I removed the escaping on the "&" characters, since it is not necessary. Note also that the escaping rules are different within alternate character [groups].

Note that I also changed your "and¦or¦phrase" to "or¦phrase" for technical terminology correctness only -- The "¦" means "OR", so if you want an "AND" function, then you need to do that with multiple RewriteConds.

Replace all broken pipe "¦" characters with a solid pipe character before use; Posting on this forum modifies the pipe characters, and patterns with broken pipes in them will not work as expected.

Jim

Mahabub

8:09 am on Dec 11, 2008 (gmt 0)

10+ Year Member



Dear g1msd & JdMorgan,

Thanks for your quick reply. I think your directions will work. I am in vacation so still i didnt get time to test. But really it makes me happy your quick reply. When i will test i will back with the recent result.

Thanks & Best Regards
Mahabub

Mahabub

8:10 am on Dec 11, 2008 (gmt 0)

10+ Year Member



Dear g1msd & JdMorgan,

Would any one please write the full codes. Cause I am not so good at url rewrite. The code which i posted take from another forum.

Thanks & Best Regards
Mahabub

[edited by: Mahabub at 8:22 am (utc) on Dec. 11, 2008]

jdMorgan

5:52 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Would any one please write the full codes.

We don't do that here -- There is simply too much demand, and not enough contributors here to support that kind of 'free coding service'. Our purpose is to educate, rather than to provide a "help desk."

We'll be quite happy to help you learn to modify your code and get it working, but it is just that: Your code.

It is also a good idea to be sure that you completely understand any code you put on your server. Be aware that this is server-configuration code; Any single typo or minor error can have potentially-devastating effects on your server's performance and/or your pages' rankings in search engine results. If you do not understand the code and all of its effects on your server and page rankings, then you would truly be better off not using the code.

Please read the Apache Forum Charter [webmasterworld.com], which contains information about how to get the most from this forum, and links to some very useful resources. Also see our Apache Forum Library [webmasterworld.com], which contains several tutorials and other useful threads. Then post specific questions in this thread.

Note that I gave you a working example for one of your most-complex rules. You should be able to 'infer' many or all of the changes needed to the simpler rules by looking at the changes made to that complex rule.

Thanks,
Jim

Mahabub

7:10 am on Dec 14, 2008 (gmt 0)

10+ Year Member



Dear g1msd & JdMorgan,

Whats wrong with the below code:

Options FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^query=([^/]+)\&search=1$
RewriteRule ^search\.php$ [domain.com...] [R=301,L]
RewriteRule ^/search/([^/]+)\.html search.php?query=$1&search=1$

Also I try the below one and its cause an unlimited loop how would i stop this.

Options FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^query=([^/]+)\&search=1$
RewriteRule ^search\.php$ [domain.info...] [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^.]+)\.html$ /search.php?query=$1&search=1 [L]

Thanks
Mahabub

[edited by: Mahabub at 7:46 am (utc) on Dec. 14, 2008]

g1smd

5:06 pm on Dec 14, 2008 (gmt 0)

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



What's wrong with it depends on what it was supposed to do, compared with what it actually does... neither of which things have been stated here.

Need a bit more to go on.

Mahabub

5:10 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



dear g1msd,

the code which i post cause an infinite loop I just want to know how can i avoid that i need some help really its urgent. Again i post the code...

Options FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^query=([^/]+)\&search=1$
RewriteRule ^search\.php$ [domain.info...] [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^.]+)\.html$ /search.php?query=$1&search=1 [L]

Thanks
Mahabub

Mahabub

5:23 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



dear g1msd,

can u suggest me any articles by which i can learn htaccess programming or any e books.

Thanks
mahabub

jdMorgan

3:29 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that the redirect and rewriterule conflict, because each rewrites the URL produced by the other. Therefore, you get a loop.

To fix it, you need to check to be sure that the "search.php" URL is being requested directly by the client, and not as a result of the search/x/html URL being rewritten to the search.php URL. To do that, change the first rule:


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /search\.php\?query=([^&]+)&search=1\ HTTP/
RewriteRule ^search\.php$ http://www.example.com/search/%1\.html? [R=301,L]

Our forum Charter contains links to useful references, and the Apache Forum section of our WebmasterWorld library contains several threads on the subject of changing dynamic to static URLs. See the links at the top of this page.

Jim

Mahabub

9:26 am on Dec 16, 2008 (gmt 0)

10+ Year Member



dear JdMorgan,

It works great. Thanks for your and g1msd continuous support. Finally I got the tricks.

Thanks
Mahabub

g1smd

8:37 pm on Dec 16, 2008 (gmt 0)

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



Apologies for vanishing for most of yesterday. Difficult to use a computer without electricity.

Someone dug up the electric cables at the end of the road, and cut us all off for most of the afternoon and part of the evening.