John Davidson

PHP When rewrite pages urls how to remove or redirect category.php?cat_id=2 urls to rewrited urls?

0 comments
Message:


I am using this URL rewriting with PHP The Folder structure for rewriting the URLs. I am done it is working fine but after rewrite the URLs then ok now i have two URLs one is simple URL like /~admin/product-category.php?cat_id=2 and second rewrites URL /~admin/product-category/men-items so how to redirect the first URL to second URL? or just the first URL should not work because of duplicates content issues.


My Project Links:


First link: http://199.192.21.232/~admin/product-category.php?cat_id=2
Rewrites link: http://199.192.21.232/~admin/product-category/men-items


Script


define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );

$rules = array(
'picture' => "/picture/(?'text'[^/]+)/(?'id'\d+)", // '/picture/some-text/51'
'album' => "/album/(?'album'[\w\-]+)", // '/album/album-slug'
'category' => "/product-category/(?'product_category'[\w\-]+)", // '/category/category-slug'
'page' => "/page/(?'page'about|contact)", // '/page/about', '/page/contact'
'post' => "/(?'post'[\w\-]+)", // '/post-slug'
'home' => "/" // '/'
);

$uri = rtrim( dirname($_SERVER["SCRIPT_NAME"]), '/' );
$uri = '/' . trim( str_replace( $uri, '', $_SERVER['REQUEST_URI'] ), '/' );
$uri = urldecode( $uri );

foreach ( $rules as $action => $rule ) {
if ( preg_match( '~^'.$rule.'$~i', $uri, $params ) ) {
include( INCLUDE_DIR . $action . '.php' );
exit();
}
}

include( INCLUDE_DIR . '404.php' );

.htaccess


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>


My Answer:
> You can use a 301 redirect in your .htaccess file to redirect the old URL to the new rewritten URL. Here is an example of how you can do this:

apache
RewriteEngine On
RewriteCond %{QUERY_STRING} cat_id=2
RewriteRule ^product-category.php$ /~admin/product-category/men-items? [L,R=301]


This code will redirect any URL that contains "cat_id=2" in the query string to the new rewritten URL. This will help prevent duplicate content issues and ensure that only the rewritten URL is accessible.

Rate this post

4 of 5 based on 2936 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us