1

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

我到处搜索,但似乎关于 yii2 和站点地图的信息非常罕见。

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

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

已经数周试图解决这个问题,但没有成功。请帮忙。

4

3 回答 3

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

您需要删除OR上述条件下的标志,否则此表达式(不是文件或不是目录)将始终为真,并且对sitemap.xml(或任何其他尚未在上述条件中排除的静态资源)的请求将被路由到frontend/web/index.php.

这两个否定条件应该是隐式 AND'd,而不是OR'd。

于 2021-12-13T11:10:38.497 回答
0

谢谢你的回答。但解决方案其实很简单,我刚刚发现。

我将我的 sitemap.xml 文件移动到 frontend/web 文件夹并且它可以工作。

于 2021-12-19T09:42:43.363 回答
0

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

另外 - 'sitemap.xml' => 'sitemap/index',在 urlManager 中。(控制器/视图)

于 2021-12-16T17:11:47.007 回答