Why .htaccess mod_rewrite does not show a real directory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 19:33:18

问题


I've written the codes below inside a .htaccess file.

RewriteEngine On
Options +FollowSymLinks

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

RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [NC,QSA,L]

I have made a control panel for administrator of my website inside admin directory too. The problem is that when I want to enter to control panel from this address www.site.com/admin, it does not show what I really want. After that, URL changes to www.site.com/admin/?p=admin.
What is the problem ?


回答1:


Conditions are applied on following rule only.
Your second rule is evaluated even if it is an existing directory or file.

If your 2 rules must respect both conditions, you have 2 possibilities.

Duplicate your conditions:

RewriteEngine On
Options +FollowSymLinks

# this rule is evaluated if not a directory AND not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p=$1&id=$2 [NC,QSA,L]

# this rule is evaluated if not a directory AND not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [NC,QSA,L]

Or simulate if/else condition by doing nothing if it's a directory/file

RewriteEngine On
Options +FollowSymLinks

# if file or directory, leave (do nothing)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]

# if we reach here, means it's not a file or directory. So rules can be evaluated
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p=$1&id=$2 [NC,QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [NC,QSA,L]


Also, this is not so important but you don't have to escape . when into square brackets: ([^/\.]+) can be ([^/.]+)



来源:https://stackoverflow.com/questions/22265844/why-htaccess-mod-rewrite-does-not-show-a-real-directory

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