Php 如何使用get参数将url重写为更干净的url
我正在制作一个小的php web应用程序。对于这个应用程序,我希望通过htaccess重写包含参数的url,从而使url对搜索引擎优化更加友好 在这个应用程序中,我有一个base index.php,其中包括来自$\u GET['page']参数的php文件的内容 例如,我希望有以下url:Php 如何使用get参数将url重写为更干净的url,php,.htaccess,url-rewriting,Php,.htaccess,Url Rewriting,我正在制作一个小的php web应用程序。对于这个应用程序,我希望通过htaccess重写包含参数的url,从而使url对搜索引擎优化更加友好 在这个应用程序中,我有一个base index.php,其中包括来自$\u GET['page']参数的php文件的内容 例如,我希望有以下url: some app.com/?page=items&category=1 对此: some-app.com/items/1 我试图为没有参数的页面创建一个干净的url 像这样: RewriteRule ^(.
some app.com/?page=items&category=1 对此:
some-app.com/items/1 我试图为没有参数的页面创建一个干净的url 像这样:
RewriteRule ^(.*)$ ?page=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^items/([0-9]+)$ items&category=$1
但是,当我对具有如下参数的页面进行尝试时:
RewriteRule ^(.*)$ ?page=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^items/([0-9]+)$ items&category=$1
它不执行任何操作,否则我会收到一个内部服务器错误
我想知道这是否可行,或者您是否可以建议一个替代方案对于实现这一点非常有用,它是一个用PHP(PHP5.3+)编写的非常简单的路由器类。
使用AltoRouter比使用Htaccess更容易,因为添加新路由和提供更干净的URL更容易 inndex.php
<?php
require("AltoRouter.php");
$router = new AltoRouter();
/* Routes */
/* $router->map(Method, Route , Target , Name) */
/* Home page */
$router->map('GET','/', 'home.php', 'home');
/* Single item page */
$router->map('GET','/item/[*:id ]', 'item.php', 'item');
/* View category */
$router->map('GET','/category/[*:id]', 'category.php', 'category');
/* Match route */
$match = $router->match();
/* Check if there is a match */
if($match) {
header("HTTP/1.1 200 OK");
require $match['target'];
} else {
header("HTTP/1.0 404 Not Found");
echo "Error 404";
}
?>
<?php
/* Get item ID from URL */
$itemId = $match["params"]["id"];
/* Continue to rest of your script, and you can use $ItemID to get item data */
?>
<?php
/* Get category ID from URL */
$categoryId = $match["params"]["id"];
/* Continue to rest of your script, and you can use $category to show all items in category */
?>
对于类别,您的URL可以是https://example.com/category/%ID%
category.php
<?php
require("AltoRouter.php");
$router = new AltoRouter();
/* Routes */
/* $router->map(Method, Route , Target , Name) */
/* Home page */
$router->map('GET','/', 'home.php', 'home');
/* Single item page */
$router->map('GET','/item/[*:id ]', 'item.php', 'item');
/* View category */
$router->map('GET','/category/[*:id]', 'category.php', 'category');
/* Match route */
$match = $router->match();
/* Check if there is a match */
if($match) {
header("HTTP/1.1 200 OK");
require $match['target'];
} else {
header("HTTP/1.0 404 Not Found");
echo "Error 404";
}
?>
<?php
/* Get item ID from URL */
$itemId = $match["params"]["id"];
/* Continue to rest of your script, and you can use $ItemID to get item data */
?>
<?php
/* Get category ID from URL */
$categoryId = $match["params"]["id"];
/* Continue to rest of your script, and you can use $category to show all items in category */
?>
所有文件都需要位于web服务器的主目录中,通常称为public\u html
。您可以将item.php
和category.php
放在另一个文件夹中,但您需要更改map中的目标,例如:
$router->map('GET','/item/[*:id ]', 'views/item.php', 'item');
这正是我需要的。非常感谢你!!