Wordpress Rewrite url

Discussion in 'PHP' started by SharingEngines, Oct 19, 2012.

  1. #1
    Hi,

    I try to change this url

    http://www.55555.com/?s=&cp_state=word to this http://www.55555.com/word

    I try to add a sting like this in functions.php

        function fb_change_search_url_rewrite() {
        if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
        }
        }
        add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
    Code (markup):

    but give me a url like this:
    http://www.55555.com/search/ instead of this http://www.55555.com/word

    Can you help me please to make this work?

    Thank you in advance for your help.
     
    SharingEngines, Oct 19, 2012 IP
  2. Imran

    Imran Notable Member

    Messages:
    2,340
    Likes Received:
    190
    Best Answers:
    0
    Trophy Points:
    230
    #2
    Hi, I tested the above code and it is working fine, with /search/word you might need to look at your template search code? as you are getting the following s=&cp_state=word, which should be ?s=word&submit=Search
    I hope it helped you a bit.
     
    Imran, Oct 20, 2012 IP
  3. SharingEngines

    SharingEngines Member

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    SharingEngines, Oct 20, 2012 IP
  4. Imran

    Imran Notable Member

    Messages:
    2,340
    Likes Received:
    190
    Best Answers:
    0
    Trophy Points:
    230
    #4
    Because of the SEO plugin I think it is difficult to have this way, even if you remove /search/ it takes to first matched search result.

    What is wrong with /search/word...
    or you can just replace /search/ with /s/
    Hope it helped a bit
     
    Imran, Oct 23, 2012 IP
  5. nsquareit

    nsquareit Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #5
    TRY THIS ONE

    The following example shows that even if you have permalinks activated, that the URL for the two search terms wordpress and consulting looks like this: bueltge.de/?s=wordpress+consulting&submit=Search. With a little function, which communicates with the redirect, you can adjust the URL. In my case the URL with the two searchterms look like this with the following function: bueltge.de/search/wordpress+consulting.

    Within WP-function wp_redirect I set the output, that the term search will be added to the home-url of your installation and added by the searchterms. There are many possibilities to adjust the URL.



    • function fb_change_search_url_rewrite() {
    • if ( is_search() && ! empty( $_GET['s'] ) ) {
    • wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
    • exit();
    • }
    • }
    • add_action( 'template_redirect', 'fb_change_search_url_rewrite' );

    Yes, it is also possible via htacces rules, but the source is an example for custom solutions on an redirect.


    • # search redirect
    • # this will take anything in the query string, minus any extraneous values, and turn them into a clean working url
    • RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
    • RewriteRule ^$ /search/%1/? [NC,R,L]

    I would recommend to put the function in a Plugin or in the functions.php of your theme. Any other or better ideas to accomplish this? Please let us know in the comment area.
     
    nsquareit, Oct 26, 2012 IP