使用网址中的单词替换进行HTACCESS重定向 [英] HTACCESS redirection with a word replacement in url

查看:141
本文介绍了使用网址中的单词替换进行HTACCESS重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个我认为是正确的reg表达式有疑问,但是它不起作用. 我想做的是重定向一堆包含特定字符串的url,如下所示:

http://www.example.com/**undesired-string**_another-stringhttp://www.example.com/**new-string**_another-string

http://www.example.com/folder/**undesired-string**/another-stringhttp://www.example.com/folder/**new-string**/another-string

I'm having trouble with this reg expression which i belive is correct, but it is not working. What im trying to do is redirect bunch of urls containing a specific string like this:

http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string

所以我在.htaccess中有以下代码:

So i have this code in the .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>

这应该将任何url中不需要的字符串替换为new-string,但是它不起作用,知道为什么吗?

This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?

谢谢

推荐答案

Marwen:试试这个:

Marwen: Try this:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 

RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?/$1 [L] 

RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC] 
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
 </IfModule>

在上面的注释的更新"代码中,您已将重写条件应用于不需要的字符串...因此,如果实际文件或目录有效,则不会重写...

In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...

尽管这样做总是会用新字符串重写不需要的字符串-即使它是一个文件名...如果可以,或者您想要什么,那么您要做的就是将重写条件移到重写以下规则...

Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...

还..只是一个F ...如果所有内容都在yoursite.com上,则无需列出yoursite.com

also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com

yoursite.com/$1new-string$2

只需要是

/$1new-string$2

具有相同功能:重写为yoursite.com的基本目录

which does the same thing: rewrites to the base directory of yoursite.com

现在,如果它们要从mysite.com转到yoursite.com,那么您将要包括域名,因为您要跨域名重定向

now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names

您可能还想使用:

[QSA,L,R=301] 

不仅是[L,R = 301]

instead of just [L,R=301]

这篇关于使用网址中的单词替换进行HTACCESS重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆