htaccess rewrite rule for admin panel

2019-07-21 11:51发布

问题:

I have the url pattern in my web application like this "www.mysitename.com/foldername/controller/method". And all requested pages are redirected to index.php first on root folder and then it process the requested pages. but whenever i go to admin panel (www.mysitename.com/admin) the url gets the "admin" as a folder name and it shows me the index.php of the admin, (thats normal in this way) then with controllername or methodname it does not redirect through the index.php of the admin folder.

I want the url in this pattern "www.mysitename.com/admin/foldername/controller/method", whenever i visit the "admin". As well as the all requested pages through admin should be redirected to the index.php on admin folder first. here is my htaccess file -

    Options -Indexes 
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

回答1:

Do you have an .htaccess file in your admin directory, or just one in the root directory? You need to have one in each directory.

I just tested this with your .htaccess in /htdocs/pro and the same .htaccess in /htdocs/pro/admin and it worked exactly as you explained.

Root htaccess in /htdocs/pro/.htaccess

Options -Indexes 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Admin htaccess in /htdocs/pro/admin/.htaccess

Options -Indexes 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Single htaccess file for both in /htdocs/.htaccess or /htdocs/pro/.htaccess

Options -Indexes
RewriteEngine on

# Change '/pro/' if directory is renamed
RewriteBase /pro/

# Change the '/pro/' portion of the condition if directory is renamed 
RewriteCond %{REQUEST_URI} ^/pro/admin(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ admin/index.php?url=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]