[英]HTACCESS redirection with a word replacement in url
I'm having trouble with this reg expression which i belive is correct, but it is not working. 我在使用此reg表达式时遇到麻烦,我认为这是正确的,但是它不起作用。 What im trying to do is redirect bunch of urls containing a specific string like this:
我想做的是重定向一堆包含特定字符串的URL,如下所示:
http://www.example.com/**undesired-string**_another-string
to http://www.example.com/**new-string**_another-string
http://www.example.com/**undesired-string**_another-string
http://www.example.com/**new-string**_another-string
http://www.example.com/**undesired-string**_another-string
到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
http://www.example.com/folder/**undesired-string**/another-string
http://www.example.com/folder/**new-string**/another-string
http://www.example.com/folder/**undesired-string**/another-string
到http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess: 所以我在.htaccess中有此代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ? 这应该在任何URL中将ANY不需要的字符串替换为new-string,但是它不起作用,为什么?
Thank you 谢谢
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... 不过,这样做总是会用新字符串重写不需要的字符串-即使它是文件名...。如果这很好或者您想要什么,那么您要做的就是将重写条件移到重写规则以下。 。
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com 也..只是一个骗子..如果所有内容都在yoursite.com上,则无需列出yoursite.com
ie 即
yoursite.com/$1new-string$2
just needs to be 只是需要
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.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 现在,如果它们从mysite.com转到yoursite.com,则您将要包含域名,因为您要跨域名重定向
Edit: You may also want to use: 编辑:您可能还想使用:
[QSA,L,R=301]
instead of just [L,R=301] 而不只是[L,R = 301]
Your regex is not really correct. 您的正则表达式不是真的正确。 Try:
尝试:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try: 或者,如果这不起作用,请尝试:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation: ^
marks the beginning; 说明:
^
表示开始; $
marks the end; $
表示结束; the first (..)
goes to $1, the second (..)
goes to $2 and so on; 第一个
(..)
达到$ 1,第二个(..)
达到$ 2,依此类推; *
is 0 or more chars; *
为0个或更多字符; +
is 1 or more chars. +
为1个或更多字符。
To answer my own question. 回答我自己的问题。 Laravel already redirects the trailing slashes.
Laravel已经重定向了斜杠。 Problem was that Laravel was installed into a sub-directory.
问题是Laravel已安装到子目录中。 I added the location of the sub-directory to the redirect.
我将子目录的位置添加到了重定向中。 My location in this case is: "/lumen/public/".
在这种情况下,我的位置是:“ / lumen / public /”。 See the fixed htaccess below.
请参阅下面的固定htaccess。
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.