.htaccess 重定向旧格式的URL以避免404错误

voj3qocg  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(112)

几天前,我改变了我的网站上的永久链接从这种格式:/index.php/year/month/postName/postName,我注意到我从保留旧结构的外部链接中得到了很多404错误。
如何使用.htaccess将点击/index.php/year/month/postName的用户重定向到/postName?因此,不要错过这些访问。
我已经尝试了这个代码,但仍然存在错误:

RedirectMatch 301 ^/index\.php/([0-9]+)/([0-9]+)/(.*)$ https://www.example.com/$3

$4也是如此。
下面是我的htaccess代码:

RewriteEngine On
AcceptPathInfo On
RewriteRule ^index\.php/\d{4}/\d\d/([^/]*) https://www.example.com/$1 [R=301,L]

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN WpFastestCache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http\:\/\/www\.example\.com\/$1 [R=301,L]
# Start WPFC Exclude
# End WPFC Exclude
# Start_WPFC_Exclude_Admin_Cookie
RewriteCond %{HTTP:Cookie} !wordpress_logged_in_[^\=]+\=El\sCorregidor|hystoriador
# End_WPFC_Exclude_Admin_Cookie
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteCond %{HTTP_USER_AGENT} !(facebookexternalhit|WhatsApp|Mediatoolkitbot)
RewriteCond %{HTTP_USER_AGENT} !(WP\sFastest\sCache\sPreload(\siPhone\sMobile)?\s*Bot)
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !(\/){2}$
RewriteCond %{REQUEST_URI} \/$
RewriteCond %{QUERY_STRING} !.+
RewriteCond %{HTTP:Cookie} !comment_author_
RewriteCond %{HTTP:Cookie} !woocommerce_items_in_cart
RewriteCond %{HTTP:Cookie} !safirmobilswitcher=mobil
RewriteCond %{HTTP:Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/all/$1/index.html -f [or]
RewriteCond /var/www/vhosts/example.com/httpdocs/wp-content/cache/all/$1/index.html -f
RewriteRule ^(.*) "/wp-content/cache/all/$1/index.html" [L]
</IfModule>
<FilesMatch "index\.(html|htm)$">
AddDefaultCharset UTF-8
<ifModule mod_headers.c>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Mon, 29 Oct 1923 20:30:00 GMT"
</ifModule>
</FilesMatch>

# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    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 %{REQUEST_URI} \/$
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
    RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP
# Con especificamos cache
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 week"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 3 months"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 3 months"
</IfModule>
# FIN
Options +FollowSymLinks
eivnm1vs

eivnm1vs1#

RedirectMatch 301 ^/index\.php/([0-9]+)/([0-9]+)/(.*)$ https://www.myweb.com/$3

看起来“OK”。(虽然第一和第二捕获组不是必需的)。然而,我怀疑你正在使用WordPress(“permalinks”),并且有现有的mod_rewrite指令,以便通过前端控制器路由请求,所以你应该使用mod_rewrite(RewriteRule)进行重定向,而不是mod_alias RedirectMatch。(mod_rewrite总是在mod_alias之前处理,尽管配置文件中的指令顺序明显不同,因此如果您将两者混合使用,可能会出现意外冲突。)
4美元也一样
$4将为空,因为正则表达式中没有第四个捕获子模式(第二个参数)。这表明规则没有做任何事情-可能与其他指令冲突。
.htaccess文件的top,* 在 * 任何现有mod_rewrite指令之前 *,尝试以下操作:

RewriteEngine On

RewriteRule ^index\.php/\d{4}/\d\d/([^/]*) https://www.example.com/$1 [R=301,L]

我假设<year>(第二个路径段)总是4位数,<month>(第三个路径段)是2位数。
首先使用302(临时)重定向进行测试,以避免潜在的缓存问题。并在测试前清除浏览器缓存。
(You不需要重复RewriteEngine指令,如果这已经在文件后面出现。)
如果您仍然收到404并且没有重定向,请尝试在.htaccess文件的顶部设置以下内容:

AcceptPathInfo On

这明确地允许/<file>/<path-info>形式的URL,例如。/index.php/<something>。(尽管.php文件通常默认允许此操作。)

相关问题