[英]HTACCESS redirection with a word replacement in url
我在使用此reg表達式時遇到麻煩,我認為這是正確的,但是它不起作用。 我想做的是重定向一堆包含特定字符串的URL,如下所示:
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
和
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
所以我在.htaccess中有此代碼:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
這應該在任何URL中將ANY不需要的字符串替換為new-string,但是它不起作用,為什么?
謝謝
馬文:試試看:
<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>
在上面注釋中的“已更新”代碼中,您已將重寫條件應用於不需要的字符串...因此,如果實際文件或目錄有效,則不會重寫...
不過,這樣做總是會用新字符串重寫不需要的字符串-即使它是文件名...。如果這很好或者您想要什么,那么您要做的就是將重寫條件移到重寫規則以下。 。
也..只是一個騙子..如果所有內容都在yoursite.com上,則無需列出yoursite.com
即
yoursite.com/$1new-string$2
只是需要
/$1new-string$2
這樣做是一樣的:重寫為yoursite.com的基本目錄
現在,如果它們從mysite.com轉到yoursite.com,則您將要包含域名,因為您要跨域名重定向
編輯:您可能還想使用:
[QSA,L,R=301]
而不只是[L,R = 301]
您的正則表達式不是真的正確。 嘗試:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
或者,如果這不起作用,請嘗試:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
說明: ^
表示開始; $
表示結束; 第一個(..)
達到$ 1,第二個(..)
達到$ 2,依此類推; *
為0個或更多字符; +
為1個或更多字符。
回答我自己的問題。 Laravel已經重定向了斜杠。 問題是Laravel已安裝到子目錄中。 我將子目錄的位置添加到了重定向中。 在這種情況下,我的位置是:“ / lumen / public /”。 請參閱下面的固定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.