【问题标题】:Is it possible to redirect the example.com/index.php?page=abc to example.com/abc?是否可以将 example.com/index.php?page=abc 重定向到 example.com/abc?
【发布时间】:2019-01-27 21:00:57
【问题描述】:

我有一个网站有https://www.example.com/index.php?page=abc 我希望这个 URL 像这样打开 https://www.example.com/teachers 我已经通过.htaccess 重定向代码尝试过,但它返回“找不到页面”。

下面是我的.htaccess 代码

RewriteCond %{QUERY_STRING} (^|&)page=teachers($|&) 
RewriteRule ^index\.php$ /teachers? [L,R=301] 

【问题讨论】:

  • 是的,但你一定是在.htaccess 编码中犯了一个错误。但是,您没有向我们展示这一点。您想将其添加到您的问题中吗?
  • 添加的代码请检查

标签: php .htaccess url url-rewriting


【解决方案1】:

对于您的教师 URL,您可以使用以下规则将干净的 url 转换为带有查询字符串的 .php 文件:

# URL
# INPUT https://www.example.com/teachers
# OUTPUT https://www.example.com/index.php?page=teachers

RewriteEngine On
RewriteRule ^(.*)$ /index.php?page=$1 [L]

查看htaccess.madewithlove.be

这允许用户访问https://www.example.com/teachers,并且在您的index.php 中您可以通过$_GET['page'] 接收teachers 字符串。

【讨论】:

  • 它返回您提到的上述 htaccess 代码的内部服务器错误
  • 如果您查看上面的 htaccess.madewithlove.be 链接,您将看到它是如何工作的并且它是有效的。一定是你的 .htaccess 中的其他东西导致了这个错误。
猜你喜欢
  • 1970-01-01
  • 2022-01-02
  • 2011-12-13
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多