A
A
Alexander Sinitsyn2018-10-16 10:09:04
Yii
Alexander Sinitsyn, 2018-10-16 10:09:04

How to make a redirect without an htaccess redirect in the root so that the controller in the frontend application works?

rule

[
    'pattern' => 'image/get/<filepath:.+>',
    'route' => 'image/get,
],

htaccess at the root
Options +FollowSymlinks
    # Включаем mod_rewrite
    RewriteEngine On

    RewriteCond %{REQUEST_URI} ^/upload [NC]
    RewriteCond %{REQUEST_URI} \.(jpg|png|gif)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^upload\/?(.+) /image/get/$1 [NC,L]

    # Если запрос начинается с /admin, то заменяем на /backend/web/
    RewriteCond %{REQUEST_URI} ^/admin [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^admin\/?(.*) /backend/web/$1 [NC,L]

    # Добавляем другой запрос /frontend/web/$1
    RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) /frontend/web/$1
htaccess in frontend/web folder
# Mod_Autoindex
<IfModule mod_autoindex.c>
  # Запрещаем просмотр содержимого папок
  Options -Indexes
</IfModule>

# Mod_Rewrite
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  # Включаем mod_rewrite
  RewriteEngine On

  # Если это папка или файл, открываем её/его
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  # В противном случае перенаправляем на index.php
  RewriteRule . index.php
</IfModule>

I
contact 1) at the address /upload/file.jpg , I get 404
2) at the address /image/get/file.jpg , I get the result of the controller's work.
3) I add R=301 to the first redirect, I get the result of the controller's work, but with a redirect, but without it.
Moreover, ["REDIRECT_URL"] in both 1 and 2 are the same
"/frontend/web/image/get/file.jpg"
"/frontend/web/image/get/file.jpg"
When you run into a wall out of the blue , you understand that you don’t understand something and it just infuriates (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-10-16
@webinar

You are coming from the wrong side. It is necessary that /upload/file.jpg would lead to image/get. There, take the file using php and give it back. Without redirects, why do you need them?

A
Alexander Sinitsyn, 2018-10-16
@a_u_sinitsin

I'll write how it works.
htaccess rewritten like this

RewriteCond %{REQUEST_URI} ^/upload [NC]
    RewriteCond %{REQUEST_URI} \.(jpg|png|gif)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.+) /frontend/web/$1 [NC,L]

Those. if the request is from upload and there is no file, then it redirects further to yii , and if it already exists, it skips it.
And I made a redirect to the controller in the routes
[
    'pattern' => 'upload/<filepath:.+>',
    'route' => 'image/get',
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question