PHP 重写页面 url 时如何删除或重定向 category.php?cat_id=2 url 到重写的 url? [英] PHP When rewrite pages urls how to remove or redirect category.php?cat_id=2 urls to rewrited urls?
问题描述
我正在使用这个 用 PHP 重写 URL 用于重写 URL 的文件夹结构.我完成了它工作正常但是在重写 URL 之后就可以了,现在我有两个 URL,一个是简单的 URL,如 /~admin/product-category.php?cat_id=2
和第二个重写 URL /~admin/product-category/men-items
那么如何将第一个 URL 重定向到第二个 URL?或者由于重复的内容问题,第一个 URL 不应该起作用.
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.
我的项目链接:
第一个链接:http://199.192.21.232/~admin/product-category.php?cat_id=2重写链接:http://199.192.21.232/~admin/product-category/men-项目
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
脚本
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>
推荐答案
这里是引用的代码示例应该如何工作:
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 ));
这篇关于PHP 重写页面 url 时如何删除或重定向 category.php?cat_id=2 url 到重写的 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!