.htaccess 301重定向中的路径相对于什么


What is the Path In .htaccess 301 Redirect Relative To?

我刚刚从头开始重新开发了一个现有的站点。旧网站使用纯HTML,而新网站将从数据库中提取内容。现在我需要将旧页面重定向到新的URL结构,但我;我对如何编写文件到文件重定向的.htaccess规则有点困惑。

在所有的例子中,我;我们发现,规则的第一部分看起来像从根目录的绝对目录路径,但它们只包含域名后面的URL部分。

例如,我想重定向

https://garrettcounty.us/archives/12262011news.html

https://garrettcounty.us/news/20111226/house-fire-on-christmas-day

从我的例子来看;我看到了(在StackOverflow和国外),我想规则应该是

redirect 301 /archives/12262011news.html https://garrettcounty.us/news/20111226/house-fire-on-christmas-day

但服务器上原始文件的实际路径是

/home/用户名/public_html/archives/12262011news.html

我应该使用目录路径还是来自域的路径?

我希望能够使用重写规则。不幸的是,最初的开发人员没有;我不使用一致的文件命名方案,所以我;我面临这样的问题

12262011news.html
Jan-19-2012-Headlines.html
State-Of-The-Union-25jan2012.html

在新模型中,我使用通过index.php引导所有内容

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

因此,如果有人知道将所有旧页面映射到新URL的更简单方法,我;我很想听。就目前情况来看,我似乎不得不一页接一页地重定向70多页。

好吧,你的旧URL没有新闻标题,所以很明显mod_rewrite无法创建它。但是重定向

https://garrettcounty.us/archives/12262011news.html

https://garrettcounty.us/news/20111226/

你可以在.htaccess:中使用这样的代码

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule ^archives/('d+)([^.]*)'.html$ https://garrettcounty.us/$2/$1/ [R=301,L,NC]

重定向指令中使用的路径:对于mod_alias或mod_rewrite,必须使用相对于DOCUMENT_ROOT的路径,而不是文件系统上的完整路径。