简体   繁体   English

通过htaccess Yii2重定向到后端

[英]Redirecting to backend via htaccess Yii2

I am trying to redirect my url to the admin ( backend ) part on domain/admin via htaccess. 我正在尝试通过htaccess将我的网址重定向到domain/admin的admin(后端)部分。 Still not very familiar with .htaccess and what I did so far is Main .htaccess in the root directory: 仍然对.htaccess不太熟悉,到目前为止,我所做的是根目录中的Main .htaccess

#adding the charset
AddDefaultCharset utf-8

#hide the structure
Options -Indexes
#if dir is symbol, follow it
Options FollowSymlinks

#engine on
RewriteEngine 

#if there is admin word in the URI go on the next rule
RewriteCond %{REQUEST_URI} ^/admin$ 
#load the backend index file, append the group to the url (QSA) and stop rewriting (L)
RewriteRule ^admin(/.+)$ /backend/web/$1 [QSA,L]

#everything different from admin ( because of the previous rule ) go on the next rule
RewriteCond %{REQUEST_URI} ^(.*)$
#load the frontend index file append the group to the url (QSA) and stop rewriting (L)
RewriteRule ^(.*)$ /frontend/web/$1 [QSA,L]

And this is my backend/frontend .htaccess ( they are same ): 这是我的后端/前端.htaccess (它们是相同的):

AddDefaultCharset utf-8

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# otherwise forward it to index.php
RewriteRule . index.php

The frontend is ok eg domain/site/about lets say but the backend domain\\admin gives 404 not found . 前端可以,例如domain/site/about ,但是后端domain\\admin给出404 not found Did I understand what i wrote right ? 我明白我写的对吗? Where is my mistake? 我的错误在哪里? Thank you in advance! 先感谢您!

Your regular expression matches only exactly the characters /admin . 您的正则表达式/ admin字符完全匹配。

Your regex matches: 您的正则表达式匹配:

  1. the start of the string with ^ ^开头的字符串
  2. exactly the characters /admin 确切的字符/admin
  3. the end of the string with $ $的字符串的结尾

If some characters are found after n then it does't match and it won't apply the rule. 如果在n之后找到一些字符,则表示不匹配,因此不会应用该规则。

If you want to match any request that starts with admin then try: 如果要匹配以admin 开头的任何请求,请尝试:

#if there is admin word in the URI go on the next rule
RewriteCond %{REQUEST_URI} ^admin(.*)$ 
#load the backend index file, append the group to the url (QSA) and stop rewriting (L)
RewriteRule ^admin(/.+)$ /backend/web/$1 [QSA,L]

That would match urls in your domain that start with admin but followed by any characters. 这将匹配您域中以admin开头但后跟任何字符的网址。

If you wanted to match any urls that contain the word admin then you need to write: 如果要匹配包含 admin一词的所有网址,则需要编写:

RewriteCond %{REQUEST_URI} ^(.*)admin(.*)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM