如何重定向.htaccess文件中的特定URL

时间:2021-10-03 10:50:52

I want to redirect specific pages only to the mobile version of my website. I can redirect the homepage with the code below but I can't find a way to redirect specific pages. I want to redirect https://www.richmond-medical.com/sm3 to http://m.richmond-medical.com/sm3 . My problem is when I type in Richmond-medical.com/sm3 on a mobile it goes to http://richmond-medical.com only. Here is my code:

我想将特定页面仅重定向到我网站的移动版本。我可以使用下面的代码重定向主页,但我找不到重定向特定页面的方法。我想将https://www.richmond-medical.com/sm3重定向到http://m.richmond-medical.com/sm3。我的问题是当我在手机上输入Richmond-medical.com/sm3时,它只会转到http://richmond-medical.com。这是我的代码:

<IfModule mod_rewrite.c>

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]

RewriteCond %{HTTP_HOST} ^www.Richmond-Medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/sm3$    
RewriteRule ^/?a/(.*)$ http://m.richmond-medical.com/$ [R=301,L]

RewriteCond %{HTTP_HOST} richmond-medical.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://m.richmond-medical.com [L,R=301]

</IfModule>

1 个解决方案

#1


0  

The working code for the above requirement is below

上述要求的工作代码如下

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/sm3$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

I have clubbed both the rule in an [OR] clause so that root domain redirects to mobile one and sm3 redirects to mobile one.

我在[OR]子句中加入了规则,以便根域重定向到移动域,sm3重定向到移动域。

The .htaccess rule can be tested at .htaccess tester

.htaccess规则可以在.htaccess测试器上测试

In case you have some new pages that needs to be redirected to mobile one you can add it to the [OR] clause like if we need sm5 to be redirected we just need to add sm5 in [OR] clause. We can also club it in the same rewrite condition using | like below.

如果你有一些需要重定向到移动的新页面,你可以将它添加到[OR]子句,就像我们需要重定向sm5一样,我们只需要在[OR]子句中添加sm5。我们也可以使用|在相同的重写条件下将其标记为如下。

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^(/sm3|/|/sm5)$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

#1


0  

The working code for the above requirement is below

上述要求的工作代码如下

RewriteEngine on

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^/sm3$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

I have clubbed both the rule in an [OR] clause so that root domain redirects to mobile one and sm3 redirects to mobile one.

我在[OR]子句中加入了规则,以便根域重定向到移动域,sm3重定向到移动域。

The .htaccess rule can be tested at .htaccess tester

.htaccess规则可以在.htaccess测试器上测试

In case you have some new pages that needs to be redirected to mobile one you can add it to the [OR] clause like if we need sm5 to be redirected we just need to add sm5 in [OR] clause. We can also club it in the same rewrite condition using | like below.

如果你有一些需要重定向到移动的新页面,你可以将它添加到[OR]子句,就像我们需要重定向sm5一样,我们只需要在[OR]子句中添加sm5。我们也可以使用|在相同的重写条件下将其标记为如下。

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]    
RewriteCond %{REQUEST_URI} ^(/sm3|/|/sm5)$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]