使用查询字符串重定向到另一个域

Redirecting to another domain with query strings

我要转移到另一个域,我也想重定向动态和静态 ULR。我尝试了很多 .htaccess 重定向规则,但它们不起作用。例如:

发件人:http://www.bercsekft.hu/motorosbolt/index.php?id=546

收件人:https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz

RewriteEngine On
RewriteCond   %{REQUEST_URI}    ^/index.php$
RewriteCond   %{QUERY_STRING}   ^id=509$
RewriteRule   ^(.*)$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301,L]

我在 .htaccess 中还有另一个规则。我不确定它们是否对重定向规则有影响:

RewriteEngine on


RewriteCond %{HTTP_HOST} ^bercsekft.hu$
RewriteRule ^(.*)$ "http\:\/\/www\.bercsekft\.hu\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^jarmu\/?$ "http\:\/\/www\.bercsekft\.hu\/motorosbolt" [R=301,L]

RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edeny\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edenybolt\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]

    RewriteBase /
    RewriteRule ^sitemap.xml$ motorosbolt/sitemap2.php [L]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php52” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php52___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

您的问题很可能是您使用的动态配置文件 (".htaccess") 的位置以及您在其中定义的路径。如果您确实需要使用动态配置文件,那么将它放在 http 服务器 DOCUMENT_ROOT 文件夹中通常是最简单的。您需要确保 http 服务器完全考虑此类文件(提示:AllowOverride 指令)并且该文件可由 http 服务器进程读取。

这是你的代码的一个稍微修改的版本,用于调整路径,我也替换了数字 ID,你的问题在那个细节上自相矛盾:

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)id=546(&|$)
RewriteRule ^/?motorosbolt/index\.php/?$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301]

最好从 302 临时重定向开始,然后在确定一切设置正确后才将其更改为 301 永久重定向。这可以防止在尝试时出现缓存问题...

此规则同样适用于 http 服务器主机配置或动态配置文件(“.htaccess”文件)。显然重写模块需要在http服务器内部加载并在http主机中启用。如果您使用动态配置文件,您需要注意它的解释在主机配置中完全启用,并且它位于主机的 DOCUMENT_ROOT 文件夹中。

一般性评论:您应该始终倾向于将此类规则放在 http 服务器主机配置中,而不是使用动态配置文件 (".htaccess")。那些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试并且它们确实减慢了 http 服务器的速度。它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则(这是一个明显的安全噩梦)的应用程序的情况下作为最后的选择提供。