Subfolders and mod-rewrite

回眸只為那壹抹淺笑 提交于 2019-12-20 04:38:38

问题


I need to alter this current re-write rule to accommodate for an admin folder. Here is my current mod-rewrite code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/([a-z]+?)/(.*)$ index.php?model=$1&view=$2&params=$3 [L,NS]

I have this folder structure:

ROOT:  http://www.domain.com/
ADMIN: http://www.domain.com/admin/

If the .htaccess file is in the "admin" folder it works correctly. I get:

URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/admin/.htaccess)
Array
(
    [model] => faq
    [view] => edit
    [params] => 13/asc
)

But, when its in the root folder I get:

URL: http://domain.com/admin/faq/edit/13/asc
(NOTE: http://domain.com/.htaccess)
Array
(
    [model] => admin
    [view] => faq
    [params] => edit/13/asc
)

I want the mod-rewrite to recognize that admin folder and is an actual directory and use a separate re-write rule.

Thanks in advance.


回答1:


Add this to your root .htaccess:

RewriteCond ^admin/
RewriteRule (whatever rule you need for the admin folder) [L]

Add this right after the RewriteEngine On. The key point here is [L] which tells mod-rewrite to stop executing the rest of the rules if this one matches. If you need more than one rule for the admin folder, just add more RewriteRule statements, but remember to only add the [L] to the last one.

That should do it.




回答2:


Add

RewriteCond ^admin/
RewriteRule (your Rule) [L]

before the other condition. That should do the trick.

Another way would be to include the condition in the rule directly via

RewriteRule ^admin/(rest_of_regex)$ (regex_stuff) [L]


来源:https://stackoverflow.com/questions/992054/subfolders-and-mod-rewrite

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