【问题标题】:HTACCESS Rewrite Rule Conflict for User and User Post用户和用户帖子的 HTACCESS 重写规则冲突
【发布时间】:2014-10-15 03:26:08
【问题描述】:


我在为两个重写规则找到正确的 reg-exp 时遇到问题。我正在检查文件名是否不是有效的目录、文件或链接,如果是这种情况,则继续执行两个重写规则。它们分别工作,但如果我将它们都打开,就会发生冲突。

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ([^/][a-zA-Z]+)/([0-9]+)$ /post.php?no=$2
RewriteRule ([^/][a-zA-Z]+)$ /profile.php?name=$1

所需的网址是;
用户帖子:...com/username/81
用户个人资料:...com/用户名

任何建议将不胜感激,
谢谢

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    RewriteCond 仅适用于下一个RewriteRule,但您有第二个RewriteCond 没有任何RewriteCond。使用此代码:

    RewriteEngine On
    
    # If the request is for a valid directory
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    # If the request is for a valid file
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    # If the request is for a valid link
    RewriteCond %{REQUEST_FILENAME} -l
    RewriteRule ^ - [L]
    
    RewriteRule ([^/][a-zA-Z]+)/([0-9]+)$ /post.php?no=$2 [L,QSA]
    
    RewriteRule ([^/][a-zA-Z]+)$ /profile.php?name=$1 [L,QSA]
    

    【讨论】:

    • 感谢 anubhava,似乎已经成功了。你能澄清一下 RewriteRule ^ - [L] 在做什么吗?再次感谢。
    • 很高兴它成功了。 RewriteRule ^ - [L] 部分基本上意味着 什么都不做 如果请求是针对有效的文件/目录/链接。
    猜你喜欢
    • 2012-06-09
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多