Title: htaccess empty search query
Last modified: August 22, 2016

---

# htaccess empty search query

 *  Resolved [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/)
 * Hi all you great people,
 * I have a special setup in my htaccess for two reasons:
    a) To catch empty search
   strings b) To view a specific page
 * A guy helped me once with this, but now I can’t figure out what’s wrong.
 * There are two lines added to my htaccess file:
 *     ```
       RewriteCond %{QUERY_STRING} ^s=$
       RewriteRule . /index.php
       ```
   
 * So right now I have this in my htaccess file for wordpress:
 *     ```
       # BEGIN WordPress
       <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /
       RewriteCond %{QUERY_STRING} ^s=$
       RewriteRule . /index.php
       RewriteRule ^index\.php$ - [L]
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /index.php
       </IfModule>
       # END WordPress
       ```
   
 * I have then modified my 404.php with the following:
    This was an easy way out
   and well, it did its job previously.
 *     ```
       <?php
       get_header();
       if($_SERVER['REQUEST_URI'] == '/s='){
       ?>
   
       <!-- Empty Search Results -->
       <div id="content">
       etc....
       <!-- Empty Search Results End Here -->
   
       <?php
       }else{
       ?>
   
       <!-- Page Not Found  -->
       <div id="content">
       etc....
       <!-- Page Not Found End here -->
   
       <?php
       }
       get_footer();
       ?>
       ```
   
 * Now when I do an empty search, I get a 500 Internal Server Error.
 * Are anyone able to help me out?
 * Thanks & Regards,
    Kenn Nielsen Denmark

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769406)
 * Site URL Please?
 * Does it work when you perform a normal search?
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769407)
 * Oh I’m sorry 🙂
 * URL –> [http://www.brokenfruit.dk](http://www.brokenfruit.dk)
 * Yes it does Sire
 * Cheers,
    Kenn
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769410)
 * What do you get when you remove this code from the 404.php
 *     ```
       if($_SERVER['REQUEST_URI'] == '/s='){
       ?>
   
       <!-- Empty Search Results -->
       <div id="content">
       etc....
       <!-- Empty Search Results End Here -->
   
       <?php
       }else{
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769414)
 * Hi Rajesh,
 * I have now removed that part of the code and nothing is different from before.
   Have a try.
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769416)
 * Ok, I guess you can put that code back to 404.php and remove the following lines
   from .htaccess
 * Delete…
 *     ```
       RewriteCond %{QUERY_STRING} ^s=$
       RewriteRule . /index.php
       RewriteRule ^index\.php$ - [L]
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769421)
 * My .htaccess is now looking like this: (default wordpress)
 *     ```
       # BEGIN WordPress
   
       <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /index.php
       </IfModule>
   
       # END WordPress
       ```
   
 * I don’t know if I left something out, but my goal is to get the empty search 
   working again and to show the correct piece of the 404.php ? 🙂
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769422)
 * Progress!
 * [http://www.brokenfruit.dk/?s=](http://www.brokenfruit.dk/?s=) does NOT throw
   an error.
 * Now, in the 404.php file
 * find…
 *     ```
       if($_SERVER['REQUEST_URI'] == '/s='){
       ```
   
 * Replace with…
 *     ```
       if( $_GET['s'] ==''){
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769425)
 * Now I have this code in 404.php
 *     ```
       <?php
       get_header();
       if( $_GET['s'] ==''){
       ?>
       ```
   
 * and this in .htaccess:
 *     ```
       # BEGIN WordPress
   
       <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteBase /
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteRule . /index.php
       </IfModule>
   
       # END WordPress
       ```
   
 * Still does not work though.. 🙁
 * Are we not missing a condition in the .htaccess file for `if( $_GET['s'] ==''){`
   to work?
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769428)
 * In your theme’s functions.php
 *     ```
       function empty_search() {
          global $wp_query;
       if($_GET['s']=='')
       {
         $wp_query->set_404();
         status_header( 404 );
         get_template_part( 404 );
       exit();
       }
       }
   
       add_action('wp','empty_search');
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769432)
 * Code added to functions.php in theme folder.
    Have a try.
 *     ```
       // Added by Kenn Nielsen to catch empty searches 
   
       function empty_search() {
          global $wp_query;
       if($_GET['s']=='')
       {
         $wp_query->set_404();
         status_header( 404 );
         get_template_part( 404 );
       exit();
       }
       }
   
       add_action('wp','empty_search');
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769434)
 * Rajesh, I now have disabled the WP Super Cache and it seemed to conflict, and
   for that I’m sorry.
 * I now get the correct piece of 404.php, but that is for all pages I visit.
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769438)
 * Here’s the final adjustment and it should all work just fine.
 * Theme’s functions.php file
 * Find…
 *     ```
       if($_GET['s']=='')
       ```
   
 * Replace with…
 *     ```
       if( isset($_GET['s']) && $_GET['s']=='')
       ```
   
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769442)
 * Done.
 * It seems to work with a single but..
 * a) When I try a invalid url [http://www.brokenfruit.dk/sdg](http://www.brokenfruit.dk/sdg)
   it return the ’empty search content’ and not the ‘not found content’.
    We should
   differentiate between empty searches and invalid urls.
 * Furthermore, how do one change the browser title to match the corresponding content?
 *  Thread Starter [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * (@sonyfreak)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769589)
 * With some further adjustments, everything is now working.
 * I can now
    a) Catch empty search strings and b) View a specific page for empty
   searches and 404 all without .htaccess mod_rewrite.
 * The solution is to modify three files.
 * funtions.php:
 *     ```
       // Added by Kenn Nielsen to catch empty searches
       // https://wordpress.org/support/topic/htaccess-empty-search-query
   
       function empty_search() {
          global $wp_query;
       if( isset($_GET['s']) && $_GET['s']=='')
       {
         $wp_query->set_404();
         status_header( 404 );
         get_template_part( 404 );
       exit();
       }
       }
   
       add_action('wp','empty_search');
       ```
   
 * 404.php:
 *     ```
       <?php
       get_header();
       if( isset($_GET['s']) && $_GET['s']==''){
       ?>
   
       <!-- Empty Search Results -->
       <div id="content">
       etc....
       <!-- Empty Search Results End Here -->
   
       <?php
       }else{
       ?>
   
       <!-- Page Not Found  -->
       <div id="content">
       etc....
       <!-- Page Not Found End here -->
   
       <?php
       }
       get_footer();
       ?>
       ```
   
 * And to change the browser title accordingly to the content, modify header.php:
   
   Add the following condition somewhere between the other title conditions, but
   before the last condition.
 *     ```
       elseif ( is_404() ) {
       	if( isset($_GET['s']) && $_GET['s']==''){
       		echo __('Empty search | ', 'theme1741'); bloginfo( 'name' );
       	}
       	else
       	{
       		echo __('Error 404 | ', 'theme1741'); bloginfo( 'name' );
       	}
       }
       ```
   
 * Thanks,
    Kenn Nielsen

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘htaccess empty search query’ is closed to new replies.

## Tags

 * [empty search](https://wordpress.org/support/topic-tag/empty-search/)
 * [htaccess](https://wordpress.org/support/topic-tag/htaccess/)
 * [mod_rewrite](https://wordpress.org/support/topic-tag/mod_rewrite/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [Kenn Nielsen](https://wordpress.org/support/users/sonyfreak/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/htaccess-empty-search-query/#post-5769589)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
