I need to add urls with query string to sitemap, for example "node?page=2" via API.
I setup $xmllink array with 'loc'='node?page=2', but via this way in XML I see "node%3Fpage%3D2" instead of normal url.
How I can correctly save the query string into xml sitemap item?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Murz’s picture

How to reproduce:

    $xmllink = array(
      'id' => 1,
      'type' => 'custom',
      'subtype' => 'custom1',
      'status' => 1,
      'changefreq' => 86400*7,
      'lastmod' => time(),
      'priority' => 0.8,
      'loc' => 'node?page=2',
      'access' => 1,
      'language' => LANGUAGE_NONE,
    );
    xmlsitemap_link_save($xmllink);

And after this we will see in sitemap.xml:

<url><loc>http://example.com/node%3Fpage%3D2</loc><lastmod>2013-02-13T18:47Z</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url>
Murz’s picture

I have solve this problem for me via this patch for function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $chunk) {

        'xmlsitemap_link' => $link,
        'xmlsitemap_sitemap' => $sitemap,
      );
+     if(strpos($link['loc'],'?')) {
+       list($link['loc'],$loc_query)=explode('?',$link['loc']);
+     } else {
+       $loc_query=NULL;
+     }
      // @todo Add a separate hook_xmlsitemap_link_url_alter() here?
      $link_url = url($link['loc'], $link_options + $url_options);
+     
+     if($loc_query)
+       $link_url .= '?'.$loc_query;
  
      // Skip this link if it was a duplicate of the last one.
      // @todo Figure out a way to do this before generation so we can report
Anonymous’s picture

Title: How to add loc with query string (get parameters) into drupal xml sitemap? » Add loc with query string (get parameters) into drupal xml sitemap link
Category: support » feature
mh86’s picture

Status: Active » Needs review
FileSize
958 bytes

Encountered the same issue (query parameter was encoded by url() and thus did not work any more).

Made a patch out of #2. Maybe there is a nicer way to do that.

mh86’s picture

Updated my patch. The $loc_query variable needs to be re-initialized on every iteration.

Anonymous’s picture

Status: Needs review » Needs work

You misspelled the word "twice" in your comments as "twiche".

mh86’s picture

+ "Readd". Two typos in two comments ;-)

Thanks for that note. The patch needs an update.

Ari Linn’s picture

Issue summary: View changes

I've run into the same problem, so thanks for telling me where to look for the issue. However, I suggest using parse_url() to determine whether a url has a query part.

    $link_parts = parse_url($link['loc']);
    // @todo Add a separate hook_xmlsitemap_link_url_alter() here?
    $link_url = url($link_parts['path'], $link_options + $url_options) 
		. (isset($link_parts['query']) ? '?' . $link_parts['query'] : '');
Murz’s picture

Status: Needs work » Needs review
FileSize
806 bytes

@Ari Linn, thanks for the hint, your version is more simple and works well. Here is updated patch with your version of solution. Please re-test it and commit to head.

wojtha’s picture

ivan.lewas’s picture

What about hash attributes? So a link like this: node/50/#comment10 can be rendered correctly.

ULikeApples’s picture

Update patch for version "7.x-2.2"

Status: Needs review » Needs work

The last submitted patch, 12: 1918478-xmlsitemap_query_param-7.patch, failed testing.

firewaller’s picture

Update patch for version 7.x-2.3. Also leverages url() query and fragment keys with drupal_parse_url() rather than appending to string.

artem_sylchuk’s picture

Status: Needs work » Needs review
FileSize
1.11 KB

@firewaller thanks for a patch, it helped me a lot.
Unfortunately you've created a p0 patch and it should be p1, see: https://www.drupal.org/node/707484
Attached the updated patch version.

pifagor’s picture

Status: Needs review » Reviewed & tested by the community

The last submitted patch, 14: xmlsitemap-query-and-fragment-1918478-8.patch, failed testing. View results

pifagor’s picture

alex_optim’s picture

Looks good

pifagor’s picture

Patch Failed to Apply, I recreate patch

  • pifagor committed 866c99b on 7.x-2.x
    Issue #1918478 by mh86, Murz, pifagor, firewaller, ULikeApples,...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.