如何根据URL的文件路径重定向URL


How do I redirect URL based on its filepath?

我需要根据URL的开头重定向所有流量,包括域名和文件路径:

所以

http://www.mywebsite.co.uk/home/this/path/stays/thesame/&http://mywebsite.co.uk/home/this/path/stays/thesame/

成为

http://minigame.mywebsite.co.uk/home/this/path/stays/thesame

因此,正如您所看到的,只有当url指向/home/目录时,才会调用重定向。

我没有写.htaccess文件的经验,除了复制和粘贴,任何帮助都将不胜感激。

此解决方案不使用.htaccess技术,但仍将为您提供功能。据我所知,这样做的唯一缺点是,你必须将代码添加到你希望重定向的页面上。

如果你有一个基于模板的系统,并且可以将其应用于一个文件,并且它会自动调整以适应每个页面,那么效果最好。

$path = $_SERVER['REQUEST_URI'];                  // Path after domain
$newdomain = 'http://minigame.mywebsite.co.uk'    // New domain for redirect
header("Location: $newdomain.$path")              // Redirect user to new location 

您可以在旧网站的/home/.htaccess文件中使用此代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www'.)?mywebsite'.co'.uk$ [NC]
RewriteRule ^ http://mywebsite.co.uk%{REQUEST_URI} [L,NE,R=302]