T.Todua
T.Todua

Reputation: 56341

.htaccess redirect directly from a subfolder

There are many questions like this, but I can’t find a solution for this exactly.

I open example.com/smth1/smth2/media_files. I want to put a .htaccess file in the media_files directory which will redirect all links to example.com/smth1/smth2/new_media_files.

Note that smth1, etc. are dynamic, so I can’t enter them hard coded in the .htaccess file.

Upvotes: 1

Views: 61

Answers (1)

anubhava
anubhava

Reputation: 784918

Inside subdir/.htaccess you can have this rule:

RewriteEngine On

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

If you want URLs to change in browser then use R (redirect) flag:

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

EDIT

As per edited question you can use this rule:

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

Upvotes: 1

Related Questions