【问题标题】:Redirecting to backend via htaccess Yii2通过 htaccess Yii2 重定向到后端
【发布时间】:2018-06-29 20:16:26
【问题描述】:

我正在尝试通过 htaccess 将我的 url 重定向到 domain/admin 上的管理员(后端)部分。仍然不是很熟悉.htaccess,到目前为止我所做的是 根目录主.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]

这是我的后端/前端.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

前端还可以,例如 domain/site/about 可以说,但后端 domain\admin 提供 404 not found。我理解我写的正确吗?我的错误在哪里?提前谢谢!

【问题讨论】:

    标签: php .htaccess redirect mod-rewrite yii2


    【解决方案1】:

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

    你的正则表达式匹配:

    1. ^开头的字符串
    2. 正是字符/admin
    3. $ 结尾的字符串

    如果在n 之后发现某些字符,则它不匹配并且不会应用规则。

    如果您想将 开始 的任何请求与 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]
    

    这将匹配您域中以 admin 开头但后跟任何字符的网址。

    如果您想匹配任何 包含 单词 admin 的 url,那么您需要编写:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多