本文介绍了当目录存在htaccess的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关制作友好的URL,以下是应用htaccess的code:

  RewriteEngine叙述在的RewriteCond%{} THE_REQUEST /.+?\\.php[\\s?] [NC]
重写规则^ - [F]重写规则^([^ / \\。] +)$的index.php?ID1 = $ 1 [NC,L,QSA]
重写规则^([^ / \\。] +)/([^ / \\。] +)$的index.php ID1 = $ 1和; ID2 = $ 16 [NC,L,QSA]

这现在工作为:


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


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


此外,当我进入 mydomain.com/index.php?id1=pages&id2=xyz 在URL中手动,它重定向到 mydomain.com

现在,当我进入另一个像 mydomain.com/templates ,其中模板是存在的目录,它重定向到 mydomain.com/templates /?ID1 =模板

编辑1:

我想加入这一行,却徒劳无功:

 的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{DOCUMENT_ROOT} / $ 1 \\ .PHP -f [NC]
重写规则^(。+?)/ $ /$1.php [L]

我该如何避免这种情况(与该名称目录存在)使用htaccess的?


解决方案

  RewriteEngine叙述在的RewriteCond%{} THE_REQUEST /.+?\\.php[\\s?] [NC]
重写规则^ - [F]
#serve迪尔斯而无需重写
的RewriteCond%{} REQUEST_FILENAME -d
重写规则^ - [L]
#####
重写规则^([^ / \\。] +)$的index.php?ID1 = $ 1 [NC,L,QSA]
重写规则^([^ / \\。] +)/([^ / \\。] +)$的index.php ID1 = $ 1和; ID2 = $ 16 [NC,L,QSA]

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?

解决方案
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] 

这篇关于当目录存在htaccess的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:36