2016-02-17 62 views
-2

我試圖從URL的末尾刪除字符串:URL重寫使用htaccess的變化的查詢字符串中的基礎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 

我想從URL的末尾轉換http://sitename.com/category/-----category-name/?post_type=question to http://sitename.com/category/-----category-name/post_type/question的網址。

+0

-1您在下面指出,您希望使用.htaccess而不是php進行重定向。因此這個問題與php無關。 –

+0

你可能會用.htaccess文件做什麼:請參閱ModRewrite。 http://stackoverflow.com/questions/16388959/url-rewriting-with-php?rq=1 – jjwdesign

回答

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被消毒。

+0

感謝您的重播,但我不想重定向的URL我只是想重寫的URL LIKE * *在 RewriteBase RewriteEngine敘述/ CP/ 的RewriteCond%{} REQUEST_FILENAME!-f 的RewriteCond%{} REQUEST_FILENAME!\ PHP的$ 重寫規則。* $ 0.php [L] ** –

+0

在此背景下,改寫爲「相同「的重定向。如果您試圖在不使用PHP的情況下重寫URL,那麼這個問題與PHP無關(即,請將PHP從這個問題的標籤中移除)。 –

相關問題