簡體   English   中英

根據路徑的開頭重定向

[英]Redirect based on the beginning of the path

我正在構建一個 Laravel 項目。 該項目本身有一個結構完全不同的“遺留”版本。 它記錄了一些用戶正在嘗試訪問在新環境中找不到的文件。

為了快速“修復”,我們希望重定向路徑

  • /media/22044/blablabla.pdf/en/press

快速的解決方案是使用

Redirect 301 /media/22044/blabla.pdf /en/press

但是我們希望/media/后面的路徑是動態的。 我是.htaccess的新手。 有辦法嗎?

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

# this one works, but not dynamic
#Redirect 301 /media/2336/blabla.pdf /en/press

# failed experiments
#Redirect 301 (\/media)(.*)$ /en/press
#Redirect 301 ^/media/(.*)$ /en/press
#RewriteRule ^/media/(.*) /en/press [R=301,NC,L]
Redirect 301 /media/(.*) /en/press
</IfModule>

在現有重寫之前,您需要使用 mod_rewrite RewriteRule指令(不是Redirect )。

例如:

RewriteEngine on

# Redirect "/media/<anything>" to "/en/press"
RewriteRule ^media/ /en/press [R=301,L]

# Rewrite all requests to the "/public" subdirectory
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Redirect指令使用簡單的前綴匹配,而不是正則表達式。 但也在請求的后面處理。

請注意,與Redirect指令不同,與RewriteRule模式匹配的 URL 路徑不以斜杠開頭。

使用您顯示的示例和嘗試,請嘗試遵循 .htaccess 規則文件。 請確保在測試您的 URL 之前清除您的瀏覽器緩存。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public [NC]
RewriteRule ^(.*)/?$ public/$1 [L,NC]

###new redirect here...
RewriteCond %{THE_REQUEST} \s/.*/media/22044/blablabla\.pdf\s [NC]
RewriteRule ^  /en/press? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en/press)/?$ media/22044/blablabla\.pdf [NC,L]
</IfModule>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM