在基本网址中使用htaccess更改查询字符串重写网址

时间:2016-02-17 18:35:16

标签: .htaccess url url-rewriting

我试图从网址末尾删除字符串:

例如我有这样的结构:

http://sitename.com/category/first-category-name.php/?post_type=question
http://sitename.com/category/second-category-name.php/?post_type=question
http://sitename.com/category/...-category-name.php/?post_type=question

我想从网址末尾的http://sitename.com/category/-----category-name/?post_type=question to http://sitename.com/category/-----category-name/post_type/question转换网址。

1 个答案:

答案 0 :(得分:1)

如果查询字符串中存在post_type值,则可以有条件地重定向。

// Get the category. This is here for example purposes.
$categoryName = 'first-category-name';

if (!empty($_GET['post_type'])) {
    $postType = $_GET['post_type'];
    header("Location: http://sitename.com/category/$categoryName/post_type/$postType", true, 301);
}

为了安全起见,您可能希望确保清除$category$postType

相关问题