【问题标题】:htaccess check if empty query stringhtaccess 检查是否为空的查询字符串
【发布时间】:2015-11-24 19:30:58
【问题描述】:

我的服务器出现以下情况:

/->
  news-> 
    .htaccess
    index.php
    post.php
...

我的 .htaccess 中有以下规则:

RewriteRule    ^(.*)/admin          post.php?slug=$1&admin_mode=true            [NC,QSA,L]
RewriteRule    ^(.*)$               post.php?slug=$1                            [NC,QSA,L]

现在我需要我的网址如下:

如果请求 www.mydomain.com/news/ -> 它应该得到 index.php 文件

如果要求 www.mydomain.com/friendly-title-of-my-article -> 它 应该获取带有查询字符串的 post.php 文件,如我的 .htaccess 中所示。

目前我使用查询字符串正确获取了 post.php,但是当我转到 www.mydomain.com/news/ 时,它正在请求 post.php 文件。

请帮忙。谢谢

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    使用这个

    #if query string is empty
    
    RewriteCond %{QUERY_STRING} ^$
    
    #match on / for root, or .index.php in request and send to query string version 
    
    RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
    

    【讨论】:

    • 已经尝试过了.. 它让我在请求 www.example.com/news/ 时转到 example.com/var/www/site/news/index.php?step=1
    • 这应该是答案。 +1 为 RewriteCond %{QUERY_STRING} ^$
    • +1 for (/|index.php),我不知道我可以在 .htaccess 中做到这一点。当没有 QUERY_STRING 存在时,我花了 1 个小时尝试重定向。
    【解决方案2】:

    创建良好路由的一种方法是让所有内容都转到一个index.php 并控制那里的流程。它具有多个优点,例如能够查询数据库,然后决定要加载哪个页面。这可以很好地影响 SEO。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2015-06-05
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多