Your Location is: Home > Php

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

From: Yemen View: 4314 Amir Alam Khan 

Question

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/category.php?cat_id=2 and second rewrites URL /~admin/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.

Second issue. How to remove the .php extention for normal pages like cart.php and checkout.php?

My Project Links:

First link: http://199.192.21.232/~admin/category.php?cat_id=2 Rewrites link: http://199.192.21.232/~admin/category/men-items Cart page: http://199.192.21.232/~admin/cart.php Checkout page: http://199.192.21.232/~admin/checkout.php

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'  => "/category/(?'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>

Best answer