-2

I'm trying to remove string from the end of URL:

For example i have a structure like this:

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

I would like to convert url from http://sitename.com/category/-----category-name/?post_type=question to http://sitename.com/category/-----category-name/post_type/question from the end of URL.

  • -1 You have indicated below that you would like to redirect using .htaccess rather than php. Therefore this question has nothing to do with php. – Leo Galleguillos Feb 17 '16 at 19:13
  • You'd probably what to do that with an .htaccess file: See ModRewrite. http://stackoverflow.com/questions/16388959/url-rewriting-with-php?rq=1 – jjwdesign Feb 17 '16 at 19:14

1 Answers1

1

You can conditionally redirect if the post_type value exists in the query string.

// 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);
}

For security, you probably want to make sure $category and $postType are sanitized.

Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43
  • Thanks for replay but I don't want to redirect the URL I just want to rewrite the URL LIKE ** RewriteEngine on RewriteBase /cp/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !\.php$ RewriteRule .* $0.php [L] ** – Balbir Singh Feb 17 '16 at 19:05
  • In this context, rewriting is the "same" as redirecting. If you're trying to rewrite the URL without using PHP, then this question has nothing to do with PHP (i.e. please remove PHP from the tags of this question). – Leo Galleguillos Feb 17 '16 at 19:09