【问题标题】:.htaccess URL re-write (sitemap.xml to sitemap.php).htaccess URL 重写(sitemap.xml 到 sitemap.php)
【发布时间】:2013-05-20 07:59:05
【问题描述】:

下面是我当前的 .htaccess 文件。我想添加另一个条件/规则,如果发出对 sitemap.xml 的请求,则改为提供 sitemap.php。请帮忙:)

Options +FollowSymLinks

<IfModule mod_rewrite.c>

   RewriteEngine On

   RewriteBase /

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d

   RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]

</IfModule>

【问题讨论】:

    标签: .htaccess url mod-rewrite


    【解决方案1】:

    确保您的 php 文件加载为 xml,使用下面的代码

    header('Content-Type: text/xml');
    

    【讨论】:

      【解决方案2】:

      重写规则 ^sitemap.xml$ public/sitemap.php [L]

      【讨论】:

        【解决方案3】:

        你可以明确地做到这一点:

        Options +FollowSymLinks
        
        <IfModule mod_rewrite.c>
          RewriteEngine On
        
          RewriteBase /
        
          RewriteRule ^sitemap.xml$ sitemap.php [L]
        
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
        </IfModule>
        

        您可能会在执行此操作时遇到问题...您应该为蜘蛛创建排除项,以便它们仍能收到 sitemap.xml

        【讨论】:

        • 不会将这些条件应用于 xml 行而不是初始重写吗?我的 htaccess 回忆有点模糊,但我会假设新规则必须在索引条件被声明之前出现
        • 点了。接得好;我没有考虑到它会在%{REQUEST_FILENAME} !-f 之后停止
        • 好的,我应该在“RewriteCond %{REQUEST_FILENAME} !-f”之后尝试这个新规则吗?我有一个动态生成站点地图的 .php 文件,但希望爬虫能够找到使用的站点地图.xml 以查看其内容。
        • Hrm... 就个人而言,如果我使用的是动态站点地图生成器(由于各种原因我根本不使用站点地图),我会将其设计为当我添加新内容时,重新蜘蛛网站并输出一个新的 .xml 文件,并且我会保持它与公共区域的连接。那么关于蜘蛛的观点就没有实际意义了。
        猜你喜欢
        • 2011-06-23
        • 1970-01-01
        • 2014-08-12
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 2011-08-30
        相关资源
        最近更新 更多