1

我必须将子文件夹的所有静态 HTML 文件重定向到新位置(旧站点的 drupal 版本):

oldsite.net/topfolder/subx  =>  newsite.net/subx

但新站点上不再存在所有文件。丢失的文件应重定向到新子文件夹中的主页

举个例子(请注意目标 URL 没有“.html”扩展名):

oldsite.net/topfolder/subx/file1.html redirect to newsite.net/subx/file1 
oldsite.net/topfolder/subx/file2.html redirect to newsite.net/subx/file2

oldsite.net/topfolder/subx/missing1.html redirect to newsite.net/subx/mainpage
oldsite.net/topfolder/subx/missing2.html redirect to newsite.net/subx/mainpage

在根 .htaccess 中,我可以为每个文件编写如下内容:

redirect 301 /topfolder/subx/file1.html  http://www.newsite.net/subx/file1

和丢失的文件

redirect 301 /topfolder/subx/missing1.html http://www.newsite.net/subx/mainpage

但是对于超过 300 个文件,这是很多工作。

我不太擅长重写规则,我正在尝试编写规则,但它似乎不起作用。

这应该适用于现有文件(我没有尝试过):

RewriteRule /topfolder/subx/^ (.*)\.html$  http://www.newsite.net/subx/$1 [R=301,L] 

但是丢失的文件怎么办?上面的规则对吗?

有什么建议吗?谢谢你的回复。

4

1 回答 1

1

通过启用 mod_rewrite 和 .htaccesshttpd.conf然后将此代码放在您的 oldsite目录.htaccess下:DOCUMENT_ROOT

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^topfolder/(subx/[^.]+)\.html$ http://www.newsite.net/$1 [R=302,L,NC]

通过启用 mod_rewritehttpd.conf和 .htaccess,然后将此代码放在您的newsite目录.htaccess下:DOCUMENT_ROOT

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# not found then strip out query string and redirect to mainpage
RewriteCond %{QUERY_STRING} ^new=1$ [NC]
RewriteRule ^ subx/mainpage? [R=301,L]

# otherwise just strip out query string
RewriteCond %{QUERY_STRING} ^new=1$ [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
于 2013-02-27T16:52:10.897 回答