I have created a sitemap.xml for my yii2 project. I put it in root folder but I can’t seem to access it with this URL: https://www.mywebsite.com/sitemap.xml. I need the URL to input it to Google Search Console.
I’ve searched everywhere but it seems that information about yii2 and sitemap are very rare.
What is the correct way to place the sitemap.xml? I believe it can be fixed in htaccess file but I don’t know how.
Options +FollowSymlinks
RewriteEngine On
# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/font/(.*)$ backend/web/font/$1 [L]
RewriteRule ^backend/plugins/(.*)$ backend/web/plugins/$1 [L]
RewriteRule ^backend/images/(.*)$ backend/web/images/$1 [L]
RewriteRule ^backend/img/(.*)$ backend/web/img/$1 [L]
RewriteRule ^backend/js/(.*)$ backend/web/js/$1 [L]
RewriteRule ^backend/app-assets/(.*)$ backend/web/app-assets/$1 [L]
RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|font|plugins|images|img|js|app-assets)/
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} /(assets|css|js|img|font|uploads)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]
#RewriteRule ^uploads/(.*)$ frontend/web/uploads/$1 [L]
RewriteRule ^uploads/(.*)$ uploads/$1 [L]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|images|font|fonts|uploads|plugins|app-assets)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
<IfModule mod_expires.c>
ExpiresActive On
# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
# Video
ExpiresByType video/webm "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
# Fonts
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
It’s been weeks trying to solve this with no success. Please help.
3
Answers
Thank you for the answers. But the solution is actually very simple, I've just found out.
I moved my sitemap.xml file to frontend/web folder and it works.
You need to remove the
OR
flag on the above condition, otherwise this expression (not a file OR not a directory) will always be true and requests forsitemap.xml
(or any other static resource not already excluded in the preceding condition) will be routed tofrontend/web/index.php
.These two negated conditions should be implicitly AND’d, not
OR
‘d.I could be nice to use some "dynamic in there":
You can make a new controller like
frontendcontrollersSitemapController
and serve it in ‘FORMAT_RAW’.Additionaly – ‘sitemap.xml’ => ‘sitemap/index’, in urlManager. (controller/view)