3

I'm building a Laravel project. The project itself has a "legacy" version with a whole different structure. It's logged that some users are trying to access a file that is not found in the new environment.

For a quick "fix", we want to redirect paths like

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

The quick solution is to use

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

But we want the path behind /media/ to be dynamic. I'm new with .htaccess stuff. Is there a way to do it?

<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>
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Aldo Adhi
  • 95
  • 3
  • 6

2 Answers2

6

You need to use a mod_rewrite RewriteRule directive (not Redirect) before your existing rewrite.

For example:

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]

The Redirect directive uses simple prefix-matching, not a regex. But is also processed later in the request.

And note that the URL-path matched by the RewriteRule pattern does not start with a slash, unlike the Redirect directive.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Ah okay, so I had to put the rule before `RewriteCond %{REQUEST_URI} !^public`. Got it! Thanks! – Aldo Adhi Jan 30 '23 at 11:13
  • @AldoAdhi Yes, the order of the mod_rewrite directives is important. If you place it after the _rewrite_ then it's simply never processed. Note that `Redirect` (mod_alias) belongs to a different module and is still processed _after_ mod_rewrite. You should not mix rewrites/redirects from both modules as you can get unexpected conflicts. – MrWhite Jan 30 '23 at 11:18
  • @AldoAdhi I assume you have another `.htaccess` file in the `/public` subdirectory? – MrWhite Jan 30 '23 at 11:23
3

With your shown samples and attempts please try following .htaccess rule file. Please make sure to clear your browser cache before testing your URLs.

<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>
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Hmm, still no luck. Are you sure with {THE_REQUEST}? I also tried with {REQUEST_URI} but still doesn't work. Also why is it media/22044/blablabla\.pdf not media/22044/blablabla.pdf? – Aldo Adhi Jan 30 '23 at 09:26
  • @AldoAdhi, ok so what you getting when you have posted my rules? – RavinderSingh13 Jan 30 '23 at 09:28
  • It still redirects to 404 page. – Aldo Adhi Jan 30 '23 at 09:31
  • So basically what I need is: `http:///media/12345/blablabla.pdf` redirect to `http:///en/press` – Aldo Adhi Jan 30 '23 at 09:36
  • @AldoAdhi, ok sure, I have edited my answer, please make sure to keep your .htaccess file along with your media folder and then try it out. – RavinderSingh13 Jan 30 '23 at 09:40
  • Oh, I think there's a misunderstanding. What I need is the path behind after `/media/` is dynamic. So like, /media/12345/blabla.pdf /media/46777/testing.pdf /media/lorem/ipsum.pdf etc... the thing is the path behind /media/ is dynamic can redirect to /en/press Is that possible? – Aldo Adhi Jan 30 '23 at 09:44
  • @AldoAdhi, ohhhh that's not possible without keeping values into the URL. Can you keep your URL like eg: `http://localhost:80/en/press/media/blabla/blabla/bla` etc? Where `blabla/blabla/bla` can change in backend to .pdf file. – RavinderSingh13 Jan 30 '23 at 09:52
  • Ah I see, thought that was possible. Thanks for your assistance. – Aldo Adhi Jan 30 '23 at 10:02
  • @AldoAdhi, do you want to do it? I could write for you then, if you ok with it. – RavinderSingh13 Jan 30 '23 at 10:05
  • 1
    I think we're good now. If it's not possible to redirect from /media/ to /en/press, I'll try another approach (not using htaccess), because it's not really wise to put all possible paths (there's a lot) into htaccess. – Aldo Adhi Jan 30 '23 at 10:17