XML Sitemap Generation Program
XML Sitemap Generator
Copy the code and create the program.
Introduction
A sitemap is a page or file that lists the structure and content of your website, helping users and search engines understand the information on your site and assist with navigation.
Although the title of this page is 'XML Sitemap Generation Program' the current page does not function as a tool for generating sitemaps.
On this page, you will find the code to generate and retrieve a sitemap in XML format. Upload the code below to your site's server and run the program when you need an XML sitemap for your site. You are free to modify the code. Please feel free to use it.
Disclaimer
Depending on your site's structure, there is a possibility that this program may not successfully generate a sitemap.
The program assumes that the site is created using UTF-8. However, even if the entire content is in UTF-8, it may not function if HTML tags do not match the program's pattern.
Risks
This program is designed to be executed in an online environment. Therefore, the following risks are associated with it.
- Risk of third-party tampering: Malicious third parties may execute the PHP program, potentially altering the root.
- Server performance: With numerous files/directories, it may consume time and memory, impacting server load.
- Impact on Googlebot: During Googlebot's reference to the sitemap, if the program is crawled while writing, correct information may not reach the bot.
Measures taken
We have taken the following measures to reduce risk:
- Storage of generated files: For enhanced security of the generated sitemap file, the program follows these steps:
- Temporary storage: Sitemap files are initially stored temporarily in a location outside the root directory, preventing direct writing or tampering by malicious third parties on the web server.
- File renaming: The generated sitemap file is renamed after temporary storage, reducing the risk of tampering by changing the original file name.
Measures to be taken
To reduce your risk, please note the following:
- Program file deletion: After sitemap generation, promptly remove the uploaded program file from the server.
- Execution in a secure environment: Be cautious about placing files on remote servers and consider running them locally if you feel they are at risk.
- Load monitoring: If server load increases, cease usage immediately to prevent performance issues.
- Measures for Googlebot: After generating the sitemap, verify crawling success in Google Search Console. Consider manually initiating crawling if needed.
- Unless you have a sitemap generation plugin like WordPress, you will need to generate the sitemap yourself. In that case, please use the code introduced here.
- Detailed information about site map structure and necessity can be found on many websites, so it is omitted here, providing only the PHP code.
- Each item in the code has comments; choose the method that suits your preferences.
- The generated XML file (.xml) looks like the following, providing a sample with the minimum required items for Google registration. Additionally, you can retrieve "Page Title," "Change Frequency," and "Priority."
・The last modification date (<lastmod>) is obtained in Coordinated Universal Time (UTC).・For example, Japan time adds 9 hours to the displayed time.
- Installation Steps:
- Prepare a directory (folder) with a name like "sitemap."
- Copy the code below and paste it into your web page editor.
- Follow the comments in the code to replace each item with content that fits your conditions.
- Name the file, for example, 'sitemapgenerator.php,' and save the edited page with the '.php' extension, not '.html.'
- Upload it to your web server. If you can access the page and see a screen similar to the one below, the generation is successful.
* Tested in a development environment with approximately 20,000 pages, the file size is about 6MB (selecting all available items in the settings), and it took approximately 3 seconds.【Type including MTML section】【Type with only PHP section】
- Open the saved "XML file" in your browser to check it, or download the saved ".xml" file to check it, and if it is generated correctly, register it inGoogle Search Console Bing Webmaster Tools , and so on.
* If the file size is large, you might not be able to open it in a browser. Download the ".xml" file from the web server for confirmation.* Depending on the browser, it might not display correctly when opened.* If you open it in a browser, the "XML Declaration" on the first line might not appear. - If using it for the first time, ensure proper generation and check for unnecessary items.
- If you encounter an "Encoding error" when accessing the page, it could be caused by specific characters.
* Escape codes can be copied by clicking.
-
Common Causes of "Encoding Error":
-
An "Encoding Error" is an error message that occurs when an XML file is not correctly encoded with a specific character encoding. The following issues may be considered:
- Invalid characters are present
[Cause] XML demands certain characters ([&][']["][>][<], etc.) to be escaped. An error occurs if these characters are not properly escaped.[Fix] Escape invalid characters or ensure they are correctly escaped.
- Correct encoding is not specified
[Cause] If the XML file declaration at the beginning, such as <?xml version="1.0"?>, is present, and the encoding attribute is not correctly specified afterward, an error occurs.[Fix] Specify the correct encoding, for example, <?xml version="1.0" encoding="UTF-8"?>.
- Declaration section is incorrect
[Cause] An error occurs if the declaration section is incorrect or if there is an error between "<?xml" and "?>".[Fix] Correct the declaration section.
- File encoding doesn't match the declaration
[Cause] An error occurs if the actual encoding of the file does not match the encoding specified in the XML declaration.[Fix] Adjust the file encoding to match the declaration.
- File is corrupted
[Cause] Errors occur if the file is not saved correctly or is corrupted.[Fix] Resave the file and ensure it can be loaded successfully.
- Invalid characters are present
- When we verified this in a test environment, when no exclusions were specified, a directory such as "sys", which is not used as a web page directory (folder) on this site, was found in the XML file. If confirmed, please specify the directory name in the 'Exclude directories' item, for example, "$excludeDirectories = ['sys']".
Alternatively, try specifying something like "$excludeMetaTags = ['NOINDEX']" in the "Exclude Meta Tags" section. The PHP code below uses "NOINDEX" as the default setting.
Character Escape Code Ampersand&➡️&Single Quote‘➡️'Double Quote“➡️"Greater Than>➡️>Less Than<➡️<
* The "font-family" in the program should be adjusted to match your own site if necessary.
This concludes our introduction to the PHP code for the XML sitemap generator.
By using this code, may you achieve effective crawling and indexing from search engines, ensuring smooth site operation.
While there are paid programs with advanced features like automatic updates, here, the focus was on providing essential functionality.
There may be errors in the wording as it is a translation from the Japanese version. We apologize for the inconvenience, but if there is an error in the wording in the code, please correct or adjust it yourself.
Your feedback on using this code is appreciated. Please note that I cannot respond to questions.