【问题标题】:.htaccess URL rewrite: file.php?param=val --> removing 'file.php?param='.htaccess URL 重写:file.php?param=val --> 删除 'file.php?param='
【发布时间】:2023-04-04 23:20:01
【问题描述】:

刚刚解决了一个校对我的帖子的问题,太棒了! :P

目前我有这个代码:

# To externally redirect /file.php?page=cat/subcat/page to /cat/subcat/page
  RewriteCond %{THE_REQUEST} file.php?page=(.*) [NC]
  RewriteRule ^ %1 [R=301,L]

# To internally forward /cat/subcat/page to /file.php?page=cat/subcat/page
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*?)/?$ file.php?page=$1 [L]

首先,我希望它在语法上是正确/高效的……我很讨厌正则表达式!


现在,访问:

/cat/subcat/page

正常工作


但是如果我访问

/file.php?page=cat/subcat/page

页面不会重定向到 cat/subcat/page。


内部/外部 refirection、regex 和 .htaccess 整体让我感到困惑,所以我不确定我应该在哪里寻找问题。如果这意味着什么,htaccess 和 file.php 文件都位于 www.website.com/test/,而不是网站根目录。

谁能指出我正确的方向?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    这是因为你的正则表达式在第一条规则中是错误的。

    试试这些规则:

      RewriteEngine On
    
      # To externally redirect /file.php?page=cat/subcat/page to /cat/subcat/page
      RewriteCond %{THE_REQUEST} /file\.php\?page=([^&\s]+) [NC]
      RewriteRule ^ /%1? [R=301,L]
    
      # To internally forward /cat/subcat/page to /file.php?page=cat/subcat/page
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.+?)/?$ file.php?page=$1 [L,QSA]
    

    【讨论】:

    • 非常感谢您完美运行,只需要添加一个?在 /%1 之后,否则将查询字符串添加到末尾,例如/querystring?page=querystring 再次感谢您的帮助
    • 很高兴知道它成功了,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多