htaccess的重定向失败时,目录中重定向

编程入门 行业动态 更新时间:2024-10-28 00:24:02
本文介绍了htaccess的重定向失败时,目录中重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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]

这现在工作为:

  • mydomain/pages 到 mydomain/index.php?id1=pages

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

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

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

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

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

    我试着加上几行,却徒劳无功:

    I tried adding these lines but in vain:

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

    我的目录结构:

    www/ .htaccess index.php <--- main router templates/ img/ css/ ... ... other_directories/ ... ...

    的index.php 应接受ID1和ID2。

    index.php should receive id1 and id2.

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

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

    注:这个问题已经在这里问 。但再次发布,因为它没有获得可观的意见,也没有回答这个问题!的

    Note: This question is already asked here. But posting again, as it didn't gain considerable views nor answered the issue!

    推荐答案

    您应该强制尾随斜线在使用重定向规则目录的前面:

    You should force a trailing slash in front of directories using a redirect rule:

    RewriteEngine On RewriteCond %{THE_REQUEST} /.+?\.php[\s?] [NC] RewriteRule ^ - [F] # add a trailing slash to directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302] RewriteRule ^([^/.]+)/?$ index.php?id1=$1 [L,QSA] RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?id1=$1&id2=$2 [L,QSA]
  • 更多推荐

    htaccess的重定向失败时,目录中重定向

    本文发布于:2023-10-31 19:23:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1547047.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重定向   目录中   htaccess

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!