wordpress - 使用 .htaccess 重新路由旧页面链接

标签 wordpress .htaccess mod-rewrite seo

我有一个网站,我们大约在 2 年前从自定义 cms 转移到 WordPress,为了恢复旧页面排名,我想设置重定向 mod_rewrite ,因为某些页面链接确实发生了变化,所以这是我的情况:

  • 旧页面不安全,所以我们转移到 https
  • WordPress 使用 W3 Total Cache 和 Yoast SEO 插件(如果有任何改变)
  • 域与以前相同
  • 最终到达网址需要尾部斜杠才能正确显示页面

  • 所以基本上我想做以下事情(注意 URL 的细微变化):
  • 重定向 http://example.com/category_onehttps://example.com/category-one/ .
  • 重定向 http://example.com/category_one/sub_page_onehttps://example.com/category-one/subpage-one/ .

  • 我设法让第一个重定向工作,但第二个没有被重写,但我的知识在这里有限,所以我很高兴有一个关于如何“链接”两个重写的工作示例。
    编辑:
    这是我现在的 .htaccess :
    ErrorDocument 401 default
    
    # BEGIN W3TC Browser Cache
    <IfModule mod_mime.c>
        AddType text/css .css
        AddType text/x-component .htc
        AddType application/x-javascript .js
        AddType application/javascript .js2
        AddType text/javascript .js3
        AddType text/x-js .js4
        AddType text/html .html .htm
        AddType text/richtext .rtf .rtx
        AddType text/plain .txt
        AddType text/xsd .xsd
        AddType text/xsl .xsl
        AddType text/xml .xml
        AddType video/asf .asf .asx .wax .wmv .wmx
        AddType video/avi .avi
        AddType image/bmp .bmp
        AddType application/java .class
        AddType video/divx .divx
        AddType application/msword .doc .docx
        AddType application/vnd.ms-fontobject .eot
        AddType application/x-msdownload .exe
        AddType image/gif .gif
        AddType application/x-gzip .gz .gzip
        AddType image/x-icon .ico
        AddType image/jpeg .jpg .jpeg .jpe
        AddType image/webp .webp
        AddType application/json .json
        AddType application/vnd.ms-access .mdb
        AddType audio/midi .mid .midi
        AddType video/quicktime .mov .qt
        AddType audio/mpeg .mp3 .m4a
        AddType video/mp4 .mp4 .m4v
        AddType video/mpeg .mpeg .mpg .mpe
        AddType video/webm .webm
        AddType application/vnd.ms-project .mpp
        AddType application/x-font-otf .otf
        AddType application/vnd.ms-opentype ._otf
        AddType application/vnd.oasis.opendocument.database .odb
        AddType application/vnd.oasis.opendocument.chart .odc
        AddType application/vnd.oasis.opendocument.formula .odf
        AddType application/vnd.oasis.opendocument.graphics .odg
        AddType application/vnd.oasis.opendocument.presentation .odp
        AddType application/vnd.oasis.opendocument.spreadsheet .ods
        AddType application/vnd.oasis.opendocument.text .odt
        AddType audio/ogg .ogg
        AddType application/pdf .pdf
        AddType image/png .png
        AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx
        AddType audio/x-realaudio .ra .ram
        AddType image/svg+xml .svg .svgz
        AddType application/x-shockwave-flash .swf
        AddType application/x-tar .tar
        AddType image/tiff .tif .tiff
        AddType application/x-font-ttf .ttf .ttc
        AddType application/vnd.ms-opentype ._ttf
        AddType audio/wav .wav
        AddType audio/wma .wma
        AddType application/vnd.ms-write .wri
        AddType application/font-woff .woff
        AddType application/font-woff2 .woff2
        AddType application/vnd.ms-excel .xla .xls .xlsx .xlt .xlw
        AddType application/zip .zip
    </IfModule>
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/css A31536000
        ExpiresByType text/x-component A31536000
        ExpiresByType application/x-javascript A31536000
        ExpiresByType application/javascript A31536000
        ExpiresByType text/javascript A31536000
        ExpiresByType text/x-js A31536000
        ExpiresByType text/html A3600
        ExpiresByType text/richtext A3600
        ExpiresByType text/plain A3600
        ExpiresByType text/xsd A3600
        ExpiresByType text/xsl A3600
        ExpiresByType text/xml A3600
        ExpiresByType video/asf A31536000
        ExpiresByType video/avi A31536000
        ExpiresByType image/bmp A31536000
        ExpiresByType application/java A31536000
        ExpiresByType video/divx A31536000
        ExpiresByType application/msword A31536000
        ExpiresByType application/vnd.ms-fontobject A31536000
        ExpiresByType application/x-msdownload A31536000
        ExpiresByType image/gif A31536000
        ExpiresByType application/x-gzip A31536000
        ExpiresByType image/x-icon A31536000
        ExpiresByType image/jpeg A31536000
        ExpiresByType image/webp A31536000
        ExpiresByType application/json A31536000
        ExpiresByType application/vnd.ms-access A31536000
        ExpiresByType audio/midi A31536000
        ExpiresByType video/quicktime A31536000
        ExpiresByType audio/mpeg A31536000
        ExpiresByType video/mp4 A31536000
        ExpiresByType video/mpeg A31536000
        ExpiresByType video/webm A31536000
        ExpiresByType application/vnd.ms-project A31536000
        ExpiresByType application/x-font-otf A31536000
        ExpiresByType application/vnd.ms-opentype A31536000
        ExpiresByType application/vnd.oasis.opendocument.database A31536000
        ExpiresByType application/vnd.oasis.opendocument.chart A31536000
        ExpiresByType application/vnd.oasis.opendocument.formula A31536000
        ExpiresByType application/vnd.oasis.opendocument.graphics A31536000
        ExpiresByType application/vnd.oasis.opendocument.presentation A31536000
        ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000
        ExpiresByType application/vnd.oasis.opendocument.text A31536000
        ExpiresByType audio/ogg A31536000
        ExpiresByType application/pdf A31536000
        ExpiresByType image/png A31536000
        ExpiresByType application/vnd.ms-powerpoint A31536000
        ExpiresByType audio/x-realaudio A31536000
        ExpiresByType image/svg+xml A31536000
        ExpiresByType application/x-shockwave-flash A31536000
        ExpiresByType application/x-tar A31536000
        ExpiresByType image/tiff A31536000
        ExpiresByType application/x-font-ttf A31536000
        ExpiresByType application/vnd.ms-opentype A31536000
        ExpiresByType audio/wav A31536000
        ExpiresByType audio/wma A31536000
        ExpiresByType application/vnd.ms-write A31536000
        ExpiresByType application/font-woff A31536000
        ExpiresByType application/font-woff2 A31536000
        ExpiresByType application/vnd.ms-excel A31536000
        ExpiresByType application/zip A31536000
    </IfModule>
    <IfModule mod_deflate.c>
            AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel
        <IfModule mod_mime.c>
            # DEFLATE by extension
            AddOutputFilter DEFLATE js css htm html xml
        </IfModule>
    </IfModule>
    <FilesMatch "\.(css|htc|less|js|js2|js3|js4|CSS|HTC|LESS|JS|JS2|JS3|JS4)$">
        FileETag MTime Size
        <IfModule mod_headers.c>
            Header set Pragma "public"
            Header append Cache-Control "public"
             Header unset Set-Cookie
        </IfModule>
    </FilesMatch>
    <FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|TXT|XSD|XSL|XML)$">
        FileETag MTime Size
        <IfModule mod_headers.c>
            Header append Vary User-Agent env=!dont-vary
            Header set Pragma "public"
            Header append Cache-Control "public"
        </IfModule>
    </FilesMatch>
    <FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|WEBP|JSON|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|WEBM|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTF|TTC|_TTF|WAV|WMA|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
        FileETag MTime Size
        <IfModule mod_headers.c>
            Header set Pragma "public"
            Header append Cache-Control "public"
             Header unset Set-Cookie
        </IfModule>
    </FilesMatch>
    <FilesMatch "\.(bmp|class|doc|docx|eot|exe|ico|json|mdb|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|pot|pps|ppt|pptx|svg|svgz|swf|tif|tiff|ttf|ttc|_ttf|wav|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|BMP|CLASS|DOC|DOCX|EOT|EXE|ICO|JSON|MDB|WEBM|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|POT|PPS|PPT|PPTX|SVG|SVGZ|SWF|TIF|TIFF|TTF|TTC|_TTF|WAV|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW)$">
        <IfModule mod_headers.c>
             Header unset Last-Modified
        </IfModule>
    </FilesMatch>
    <IfModule mod_headers.c>
        Header set Referrer-Policy ""
    </IfModule>
    # END W3TC Browser Cache
    
    # BEGIN Old Site Redirects
    Redirect 301 /category_one/sub_page_one/ /category-one/subpage-one/
    Redirect 301 /category_one/ /category-one/
    
    Redirect 301 /category_two/ /category-two/
    Redirect 301 /category_two/sub_page_two/ /category-two/subpage-two/
    # END Old Site Redirects
    
    # BEGIN W3TC Page Cache core
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{HTTPS} =on
        RewriteRule .* - [E=W3TC_SSL:_ssl]
        RewriteCond %{SERVER_PORT} =443
        RewriteRule .* - [E=W3TC_SSL:_ssl]
        RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]
        RewriteRule .* - [E=W3TC_SSL:_ssl]
        RewriteCond %{HTTP:Accept-Encoding} gzip
        RewriteRule .* - [E=W3TC_ENC:_gzip]
        RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
        RewriteRule .* - [E=W3TC_PREVIEW:_preview]
        RewriteCond %{REQUEST_METHOD} !=POST
        RewriteCond %{QUERY_STRING} =""
        RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
        RewriteCond %{REQUEST_URI} \/$
        RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -F
        RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
    </IfModule>
    # END W3TC Page Cache core
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    
    
    如您所见,我尝试使用普通 Redirect ,但会发生 URL http://example.com/category_one/sub_page_one/重定向到 https://example.com/category-one/sub_page_one/ ,所以想要的结果只是部分的。

    最佳答案

    I managed to get the first of those redirects working


    虽然.htaccess中什么都没有您发布的文件似乎是这样做的?

    As you can see, I tried with normal Redirect, but what happens is URL http://example.com/category_one/sub_page_one/ redirects to https://example.com/category-one/sub_page_one/


    同样,您在问题中发布的指令不会执行这样的重定向?!没有什么专门将 HTTP 重定向到 HTTPS 的吗?因此,如果您看到这样的重定向,您要么看到缓存的响应,要么 WordPress 本身正在执行此重定向。 (尽管 category-two 重定向会产生这样的结果,因为它们的顺序错误——见下文)。
    (旁白:在您的示例 URL 中,您似乎在 URL 路径中将下划线 ( _ ) 转换为连字符 ( - ),但是,您没有将其声明为要求,并且您没有在最后一个 (编辑)示例仍然包含下划线。因此,我认为这不是特定要求,源 URL 和目标 URL 可以完全不同。)
    你应该避免混合 mod_alias Redirect指令和 mod_rewrite RewriteRule指令。更简单的Redirect指令是前缀匹配,并且总是在 mod-rewrite 之后执行,尽管在配置文件中有明显的顺序。
    这些指令的顺序也很重要。它们可能需要按特定顺序排列(取决于使用的指令和/或正则表达式)。如果有任何歧义,那么最具体的规则需要放在第一位。
    # BEGIN Old Site Redirects
    Redirect 301 /category_one/sub_page_one/ /category-one/subpage-one/
    Redirect 301 /category_one/ /category-one/
    
    Redirect 301 /category_two/ /category-two/
    Redirect 301 /category_two/sub_page_two/ /category-two/subpage-two/
    # END Old Site Redirects
    

    由于 Redirect 的前缀匹配性质指令,最后两条规则的顺序错误。第 4 条规则将永远不会被处理,并且请求 /category_two/sub_page_two/将被重定向到 /category-two/sub_page_two/ (匹配后的所有内容都附加到目标)。
    尝试将以上更改为以下内容:
    # BEGIN Old Site Redirects
    RewriteRule ^category_one/?$ https://example.com/category-one/ [R=301,L]
    RewriteRule ^category_one/sub_page_one/?$ https://example.com/category-one/subpage-one/ [R=301,L]
    
    RewriteRule ^category_two/?$ https://example.com/category-two/ [R=301,L]
    RewriteRule ^category_two/sub_page_two/?$ https://example.com/category-two/subpage-two/ [R=301,L]
    # END Old Site Redirects
    
    RewriteRule 上没有斜线前缀用于 .htaccess 时的模式.正则表达式 /?$允许在请求的 URL 上使用可选的尾部斜杠。
    这些指令还强制使用 HTTPS。 (而不是允许 WordPress 稍后执行此操作(第二次重定向)-如果它是这样配置的。)
    由于这些指令匹配特定的 URL,因此顺序并不重要。
    首先使用 302(临时)重定向进行测试,以避免与 301(永久)重定向相关的潜在缓存问题。
    您需要在测试前清除浏览器缓存。
    但是,如果您有许多“旧” URL,那么稍后在您的应用程序逻辑中处理它会更有效(即,当您的应用程序确定请求无效时)。

    we moved from custom cms to WordPress about 2 years ago


    在过去 2 年中,您没有从“旧”网址重定向到"new"网址?让“旧页面排名恢复”可能有点晚了。您是否(从您的日志中)确认仍在请求这些“旧”网址?

    关于wordpress - 使用 .htaccess 重新路由旧页面链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63951542/

    相关文章:

    css - 覆盖默认 CSS 样式

    php - 为什么我不能在函数后获取数组键值?

    apache - 如何将特定链接重定向到 https ://everything else to http://using . htaccess?

    .htaccess - 如果通过特定端口访问,如何将域重定向到不同的 IP?

    wordpress - X 机器人标签 noindex 特定页面

    php - Linkedin 共享网址/不解析开放图

    javascript - 单击后如何更改 <a> 的文本?

    .htaccess - 修复 IP 规范化错误

    apache - PHP中查询字符串的HTACCESS重写规则

    .htaccess - 从 url .htaccess 重写/重定向特定单词