首页 > 解决方案 > 如何使用问号和标签重定向或重写 url?

问题描述

我会通过.htaccess文件将以前的站点 URL 重定向或重写到相关的新站点 URL。

由于其他一些原因,这已经在.htaccess文件中,下面的代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我想重定向或重写如下:

http://www.example.com/index.php?pages/oldpage#part

http://www.example.com/newpage/

要进行此重定向或重写,我不知道要在.htaccess文件中添加什么?

标签: .htaccessurl-rewritingurl-redirection

解决方案


RewriteCond %{QUERY_STRING} ^pages/oldpage$
RewriteRule ^index\.php /newpage [QSD]
  • 使用RewriteCond匹配从开始到结束的查询字符串来捕获查询字符串。pages/oldpage
  • 用于RewriteRule匹配您的,REQUEST_URI然后将其重定向到使用QSD有效地丢弃查询字符串的位置。index.phphttp://www.example.com/index.php?pages/oldpage/newpagehttp://www.example.com/newpage

推荐阅读