php - Htaccess code to remove id and title from URLs? -
this question has answer here:
i have used .htaccess file add www.
index url , remove .php
, remove ?id=
urls.
this original url:
www.site.com/article.php?id=12&title=title-text
url .htaccess code
www.site.com/article/12
this code have removed &title=title-text
url.
how remove &title=
without title-text
? this:
www.site.com/article/12/title-text
.htaccess file
options +followsymlinks rewriteengine on rewritebase / rewritecond %{http_host} ^ifeelvideos.com [nc] rewriterule ^(.*)$ http://www.ifeelvideos.com/$1 [l,r=301] # externally redirect /dir/foo.php?id=123 /dir/foo rewritecond %{the_request} ^get\s([^.]+)\.php\?id=([^&\s]+) [nc] rewriterule ^ %1/%2? [r,l] # internally forward /dir/foo/12 /dir/foo.php?id=12 rewritecond %{request_filename} !-d rewritecond %{request_filename}.php -f rewriterule ^(.+?)/([^/]+)/?$ $1.php?id=$2 [l,qsa] # externally redirect /dir/foo.php /dir/foo rewritecond %{the_request} ^get\s([^.]+)\.php\s [nc] rewriterule ^ %1 [r,l] # internally forward /dir/foo /dir/foo.php rewritecond %{request_filename} !-d rewritecond %{request_filename}.php -f rewritecond %{query_string} ^$ rewriterule ^(.*?)/?$ $1.php [l] #alternate default index pages directoryindex first.html index.htm index.html index.php
i assuming article.php doesn't need title operate, id. can add title url if title exists in url in first place. can leave out second rule if title
in url.
# externally redirect /dir/foo.php?id=123&title=456 /dir/123/456 rewritecond %{the_request} ^get\s([^.]+)\.php\?id=([^&]+)&title=([^&\s]+) [nc] rewriterule ^ %1/%2/%3? [r,l] # externally redirect /dir/foo.php?id=123 /dir/123 rewritecond %{the_request} ^get\s([^.]+)\.php\?id=([^&\s]+)\s [nc] rewriterule ^ %1/%2? [r,l] # internally forward /dir/foo/12 /dir/foo.php?id=12 rewritecond %{request_filename} !-d rewritecond %{request_filename}.php -f rewriterule ^(.+?)/([^/]+)(/[^/]+)?/?$ $1.php?id=$2 [l,qsa]
furthermore, article.php should check $_server['request_uri']
, perform redirect if title not in it:
<?php $id = intval( $_get['id'] ); $title = titlebyid( $id ); $parsedurl = explode( "/", $url ); if( $parsedurl[3] != $title ) { header( "location: http://www.example.com/article/$id/$title", true, 301 ); exit(); }