【问题标题】:Redirecting to another domain with query strings使用查询字符串重定向到另一个域
【发布时间】:2019-03-30 10:19:15
【问题描述】:

我正在移动到另一个域,我也想重定向动态和静态 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\/$1" [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 redirect query-string


    【解决方案1】:

    您的问题可能是您使用的动态配置文件(“.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 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供。

    【讨论】:

    • 感谢您的好建议。我猜问题是我一周前做的 301 重定向。
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 2020-05-29
    • 2011-09-18
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多