问题
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]
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
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!
回答1:
Your regular expression matches only exactly the characters /admin.
Your regex matches:
- the start of the string with
^
- exactly the characters
/admin
- 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.
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]
That would match urls in your domain that start with admin but followed by any characters.
If you wanted to match any urls that contain the word admin then you need to write:
RewriteCond %{REQUEST_URI} ^(.*)admin(.*)$
来源:https://stackoverflow.com/questions/48360584/redirecting-to-backend-via-htaccess-yii2