无法访问位于根文件夹中的 yii2 项目中的站点地图

Cannot access sitemap in yii2 project placed in root folder

我已经为我的 yii2 项目创建了一个 sitemap.xml。我将它放在根文件夹中,但我似乎无法使用此 URL: https://www.mywebsite.com/sitemap.xml 访问它。我需要 URL 将其输入到 Google Search Console。

到处找了,好像yii2和sitemap的资料很少

放置 sitemap.xml 的正确方法是什么?我相信它可以在 htaccess 文件中修复,但我不知道如何。

Options +FollowSymlinks
RewriteEngine On

# deal with backend first
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/ [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/ [L]
RewriteRule ^backend/font/(.*)$ backend/web/font/ [L]
RewriteRule ^backend/plugins/(.*)$ backend/web/plugins/ [L]
RewriteRule ^backend/images/(.*)$ backend/web/images/ [L]
RewriteRule ^backend/img/(.*)$ backend/web/img/ [L]
RewriteRule ^backend/js/(.*)$ backend/web/js/ [L]
RewriteRule ^backend/app-assets/(.*)$ backend/web/app-assets/ [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/ [L]
RewriteRule ^css/(.*)$ frontend/web/css/ [L]
RewriteRule ^js/(.*)$ frontend/web/js/ [L]
RewriteRule ^img/(.*)$ frontend/web/img/ [L]
#RewriteRule ^uploads/(.*)$ frontend/web/uploads/ [L]
RewriteRule ^uploads/(.*)$ uploads/ [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

数周以来一直试图解决这个问题,但没有成功。请帮忙

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

您需要删除上述条件中的 OR 标志,否则此表达式 (not a file OR not a directory) 将 always 为真,对 sitemap.xml 的请求(或任何其他未在前面的 条件 中排除的静态资源)将被路由到 frontend/web/index.php

这两个取反条件应该是隐式的AND,而不是OR

我可以很好地使用一些“动态”: 您可以制作一个新的控制器,例如 frontend\controllers\SitemapController 并在 'FORMAT_RAW' 中提供它。

另外 - 'sitemap.xml' => 'sitemap/index',在 urlManager 中。 (controller/view)

感谢您的回答。不过解决方法其实很简单,我也是刚刚才知道。

我将我的 sitemap.xml 文件移动到 frontend/web 文件夹并且它有效。