【问题标题】:if url not exist htaccess rewrite如果 url 不存在 htaccess 重写
【发布时间】:2013-09-19 08:45:37
【问题描述】:

我是 htaccess 的新手..

我有这样的条件..

如果用户访问

first/second/third

等于

first.api.php?rquest=second&attr=third

但是如果 rquest 的值为 'index',它会从 URL 中删除,比如

first/third

等于

first.api.php?rquest=index&attr=third

不是

first/index/third

我整晚都在寻找解决方案,但没有结果。 我知道如果请求是索引但 attr 有值,系统会感到困惑,它会将 attr 视为 rquest。见上文,第一/第三,系统会将“第三”段视为请求。

可以这样重写url吗?

这是我整晚的工作,我知道这仍然是废话..

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} !rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.api.php?rquest=$2&attr=$3 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ $1.api.php?rquest=$2 [QSA]

RewriteCond %{REQUEST_FILENAME} -s
RewriteCond %{QUERY_STRING} rquest=index
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index&attr=$2 [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.api.php?rquest=index [QSA,L]

</IfModule>

【问题讨论】:

    标签: .htaccess url mod-rewrite indexing query-string


    【解决方案1】:

    试试这样的:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=$2&attr=$3 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /$1.api.php?rquest=index&attr=$2 [L,QSA]
    

    不清楚你想用旧的重写规则实现什么,但上面应该做你所描述的。

    【讨论】:

    • 嗨,乔恩,感谢您的快速响应。我已经尝试过了,现在使用 attr 的 rquest 'index' 已修复。但是另一个请求,所以它不起作用。我认为系统跳过 rquest=$2 并且更喜欢阅读 rquest 'index'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多