我最近更新了一个旧网站,我希望将所有旧网页301添加到每个旧网页的新版本中。
这是我的.htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine On
#
# below are the 301 redirects
# **rules**
#
# below are the RewriteCond
# **rules**
#
# other stuff
#
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
问题是某些旧链接的扩展名为oldfile.html
,其他链接只是文件夹oldfolder/
。
在新版本中,有products
,products/category
和products/category/subcategory
。这些网址是使用RewriteCond
生成的。
以下规则有效:
Redirect 301 /category/ /products/category
以下操作不起作用,而是将我重定向到主页。
Redirect 301 /old-folder/subcategory.html /products/category/subcategory
# /products/category/subcategory is also a RewriteRule
以下是.htaccess
# 301 rule
Redirect 301 /arredo-urbano/blocchi-standard.html /prodotti/arredo-urbano/001-blocchi-standard
# RewriteRule
RewriteRule ^prodotti/([^/]+)/([^/]+)/?$ prodotti.php?category=$1&subcategory=$2 [L]
我尝试在每个[L]
之后添加Redirect 301
标记,以便忽略.htaccess的其余部分,但这会导致500 Internal Error
所有重定向都是手动声明的,没有正则表达式或通配符,并且拼写正确。
有没有办法让所有重定向按预期工作?另外我不明白的是[L]
标志导致内部错误的原因。
谢谢
答案 0 :(得分:3)
关于一般重定向规则的三条建议:
Redirect
(mod_alias)规则与RewriteRule
(mod_rewrite)规则混合使用。在.htaccess中试用这些规则:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^category/?$ /products/category [L,NC,R=301]
RewriteRule ^old-folder/subcategory\.html$ /products/category/subcategory [L,NC,R=301]
#
# below are the 301 redirects
# **rules**
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
#
# below are the RewriteCond
# **rules**
#
# other stuff
#
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
确保在测试此更改之前清除浏览器缓存。