htaccess redirect when directory exists [duplicate]

∥☆過路亽.° 提交于 2019-12-25 05:19:23

问题


For making friendly URLs, Following is the htaccess code applied:

RewriteEngine On

RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC]
RewriteRule ^ - [F]

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

This now works for:

  1. mydomain.com/pages to mydomain.com/index.php?id1=pages

  2. mydomain.com/pages/xyz to mydomain.com/index.php?id1=pages&id2=xyz

also, when I enter mydomain.com/index.php?id1=pages&id2=xyz manually in the URL, it redirects to mydomain.com.

Now, when I enter another like mydomain.com/templates where templates is a directory that exists, it redirects to mydomain.com/templates/?id1=templates

Edit 1:

I tried adding this line but in vain:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

How shall I avoid this condition (when directory with that name exists) using htaccess?


回答1:


RewriteEngine On

RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC]
RewriteRule ^ - [F]
#serve dirs without rewriting
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]    
#####
RewriteRule    ^([^/\.]+)$    index.php?id1=$1    [NC,L,QSA]  
RewriteRule    ^([^/\.]+)/([^/\.]+)$    index.php?id1=$1&id2=$2    [NC,L,QSA] 


来源:https://stackoverflow.com/questions/36835920/htaccess-redirect-when-directory-exists

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