【问题标题】:Url rewrite using htaccess change query string in base url在基本 url 中使用 htaccess 更改查询字符串重写 URL
【发布时间】:2016-02-17 18:35:16
【问题描述】:

我正在尝试从 URL 末尾删除字符串:

例如我有这样的结构:

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 您已在下面指出您希望使用 .htaccess 而不是 php 进行重定向。因此这个问题与php无关。
  • 您可能会使用 .htaccess 文件来执行此操作:请参阅 ModRewrite。 stackoverflow.com/questions/16388959/…

标签: .htaccess url url-rewriting


【解决方案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 进行消毒。

【讨论】:

  • 感谢重播,但我不想重定向 URL 我只想重写 URL LIKE ** RewriteEngine on RewriteBase /cp/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !\.php$ RewriteRule .* $0.php [L] **
  • 在这种情况下,重写与重定向“相同”。如果您尝试在不使用 PHP 的情况下重写 URL,那么这个问题与 PHP 无关(即请从这个问题的标签中删除 PHP)。
猜你喜欢
  • 1970-01-01
  • 2012-11-15
  • 2023-03-04
  • 2012-06-16
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2016-02-13
  • 2020-06-04
相关资源
最近更新 更多