2016-08-03 54 views
1

有很多這樣的問題,但我無法準確找到解決方案。我打開example.com/smth1/smth2/media_files。我想將.htaccess文件放在media_files目錄中,該目錄將所有鏈接重定向到example.com/smth1/smth2/new_media_files.htaccess直接從子文件夾中重定向

請注意,smth1等是動態的,所以我不能在.htaccess文件中輸入硬編碼。

回答

1

裏面subdir/.htaccess你可以有這樣的規則:

RewriteEngine On 

RewriteRule ^(media_files)(/.*)?$ /$1$2 [L,NC] 

如果您希望網址在瀏覽器更改然後使用R(重定向)標誌:

RewriteRule ^(media_files)(/.*)?$ /$1$2 [L,NC,R=301] 

編輯

根據編輯過的您可以使用此規則:

RewriteCond %{REQUEST_URI} ^(.*)/media_files(/.*)?$ [NC] 
RewriteRule^%1/new_media_files%2 [L,R=301] 
+0

以及如何重寫到相同的域子文件夾?即我想'media_files'的內容被重定向到兄弟文件夾中,名爲'new_media_files' –

+1

然後使用:'RewriteRule^media_files(/.*)?$/new_media_files $ 1 [L,NC]' – anubhava

+0

非常感謝。最後一個問題: 如果url是:'/ smth/another1/another2/media_files /'並且想重定向'/ smth/another1/another2/new_media_files /'那麼我應該使用靜態短語:'/ smth/another1/another2' ?但如果父文件夾是動態的,我只知道,'new_media_files'駐留在相同的位置,'media_files'在這裏.. –

相關問題