通过htaccess Yii2重定向到后端 [英] Redirecting to backend via htaccess Yii2

查看:150
本文介绍了通过htaccess Yii2重定向到后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过htaccess将URL重定向到 domain / admin 上的admin(后端)部分。仍然对 .htaccess 不太熟悉,到目前为止,我所做的是
根目录下的Main .htaccess 目录:

I am trying to redirect my url to the admin ( backend ) part on domain/admin via htaccess. Still not very familiar with .htaccess and what I did so far is Main .htaccess in the root directory:

#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]

这是我的后端/前端 .htaccess (他们是山姆e):

And this is my backend/frontend .htaccess ( they are same ):

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

前端还可以,例如 domain / site / about 可以说,但是后端 domain\admin 给出了 404 not found 。我明白我写的对吗?我的错误在哪里?预先谢谢您!

The frontend is ok e.g domain/site/about lets say but the backend domain\admin gives 404 not found. Did I understand what i wrote right ? Where is my mistake? Thank you in advance!

推荐答案

您的正则表达式与完全匹配字符 / admin

Your regular expression matches only exactly the characters /admin.

您的正则表达式匹配项:

Your regex matches:


  1. ^

  2. 完全是字符 / admin
  3. $的字符串b $ b
  4. 带有 $
  5. 的字符串的结尾
  1. the start of the string with ^
  2. exactly the characters /admin
  3. the end of the string with $

如果在 n 之后发现一些字符,则该字符不匹配,因此不会应用该规则。

If some characters are found after n then it does't match and it won't apply the rule.

如果您想将开始 admin 的任何请求匹配,然后尝试:

If you want to match any request that starts with admin then try:

#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]

这将匹配您域中以 admin ,但后面跟任何字符。

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:

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

这篇关于通过htaccess Yii2重定向到后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆