htaccess rewrite rule for admin panel

守給你的承諾、 提交于 2019-12-12 19:17:08

问题


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]


来源:https://stackoverflow.com/questions/15001934/htaccess-rewrite-rule-for-admin-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!