【问题标题】:WordPress with custom rewrite rules in .htaccess throws a 404 error.htaccess 中具有自定义重写规则的 WordPress 引发 404 错误
【发布时间】:2015-01-13 20:00:47
【问题描述】:

我想显示这个网址:http://domain.com/highlights-this-week.html 而不是这个http://domain.com/index.php?highlights-this-week=1,但我收到 404 错误。

任何想法我做错了什么?没看懂。

我的完整 .htaccess 文件

  # GZip 
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
        AddOutputFilterByType DEFLATE application/javascript application/x-javascript
        AddOutputFilterByType DEFLATE text/xml application/xml text/x-component application/json
        AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
        AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject font/truetype application/x-font-ttf font/opentype application/x-font-woff
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </IfModule>
    # Proxy/Cache
    <IfModule mod_expires.c>
        ExpiresActive on
    # Perhaps better to whitelist expires rules? Perhaps.
        ExpiresDefault                          "access plus 1 month 1 hour"
    # cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
        ExpiresByType text/cache-manifest       "access plus 0 seconds"
    # Your document html
        ExpiresByType text/html                 "access plus 0 seconds"
    # Data
        ExpiresByType text/xml                  "access plus 0 seconds"
        ExpiresByType application/xml           "access plus 0 seconds"
        ExpiresByType application/json          "access plus 0 seconds"
    # Feed
        ExpiresByType application/rss+xml       "access plus 1 month 1 hour"
        ExpiresByType application/atom+xml      "access plus 1 month 1 hour"
    # Favicon (cannot be renamed)
        ExpiresByType image/x-icon              "access plus 1 week 1 hour"
    # Media: images, video, audio
        ExpiresByType image/gif                 "access plus 1 month 1 hour"
        ExpiresByType image/png                 "access plus 1 month 1 hour"
        ExpiresByType image/jpg                 "access plus 1 month 1 hour"
        ExpiresByType image/jpeg                "access plus 1 month 1 hour"
        ExpiresByType video/ogg                 "access plus 1 month 1 hour"
        ExpiresByType audio/ogg                 "access plus 1 month 1 hour"
        ExpiresByType video/mp4                 "access plus 1 month 1 hour"
        ExpiresByType video/webm                "access plus 1 month 1 hour"
    # HTC files  (css3pie)
        ExpiresByType text/x-component          "access plus 1 month 1 hour"
    # Webfonts
        ExpiresByType font/truetype             "access plus 1 month 1 hour"
        ExpiresByType application/x-font-ttf    "access plus 1 month 1 hour"
        ExpiresByType font/opentype             "access plus 1 month 1 hour"
        ExpiresByType application/x-font-woff   "access plus 1 month 1 hour"
        ExpiresByType image/svg+xml             "access plus 1 month 1 hour"
        ExpiresByType application/vnd.ms-fontobject "access plus 1 month 1 hour"
    # CSS and JavaScript
        ExpiresByType text/css "access plus 1 month 1 hour"
        ExpiresByType text/x-js "access plus 7 days 1 hour"
        ExpiresByType text/javascript "access plus 7 days 1 hour"
        ExpiresByType application/javascript "access plus 7 days 1 hour"
        ExpiresByType application/x-javascript "access plus 7 days 1 hour"
    # Header
        <IfModule mod_headers.c>
            Header append Cache-Control "public"
        </IfModule>
    </IfModule>
    
     
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^www\.(.*)$
    RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
    
    RewriteRule  ^highlights-this-week\.html$ index.php?highlights-this-week=1 [L]
    
    RewriteRule ^index\.php$ - [L]
    RewriteRule ([-a-zA-Z0-9]+)/$ http://domain.com/$1.html [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    </IfModule>

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite seo http-status-code-404


    【解决方案1】:

    你应该使用WordPress Rewrite API函数add_rewrite_rule()

    示例

    add_rewrite_rule('^highlights-this-week\.html$','index.php?highlights-this-week=1','top');
    

    您可以将其添加到插件或主题 functions.php 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2018-03-20
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      相关资源
      最近更新 更多