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

懵懂的女人 提交于 2020-11-29 19:21:34

问题


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>

回答1:


Here is how the referred code example supposed to work:

product-category.php

<?php
define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );

$rules = array( 
    'redirect-category'  => "/product-category\\.php\\?cat_id=(?'category'\\d+)"
);

$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode( $uri );

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

redirect-category.php

<?php
$categories = array(
    2 => 'men-items'
);
header("Location: " . preg_replace( '#'.$rule.'#', "/product_category/{$categories[$params['category']]}/", $uri ));



回答2:


At the top of your htaccess , put the following rule :

RewriteEngine on

RewriteCond %{QUERY_STRING} ^cat_id=2$ [NC]
RewriteRule category.php$ http://199.192.21.232/~admin/product-category/men-items? [L,R=302]

This will redirect http://199.192.21.232/~admin/product-category.php?cat_id=2 to http://199.192.21.232/~admin/product-category/men-items .

In case you have multiple old URLs with the same queryString , just change the condition pattern from ^cat_id=2$ to ^cat_id=.+$ . When you are sure the redirect is working fine change R=302 to R=301 to make the Redirection permanent .



来源:https://stackoverflow.com/questions/64835027/php-when-rewrite-pages-urls-how-to-remove-or-redirect-category-phpcat-id-2-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!