我创建了自己的简单帖子管理,我希望在像WordPress这样的数据库查询时更改URL路径。
例如
来自:http://example.com/bob-section/post.php?postlink=how-to-sing-like-barber-with-bieber
到此:http://example.com/bob-section/how-to-sing-like-barber
这是我目前的.htaccess
#Redirect to non www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
答案 0 :(得分:1)
您是否考虑过实施路由器?这将允许您将请求URI用作由' /'分割的变量。符号
我个人更喜欢Aura Router,因为Aura框架允许您只选择您需要的部分,而不是要求您仅为少数功能下载和使用整个框架。
要开始使用Aura,您可以下载该软件包的git存储库,并包含' bootstrap.php',或者您可以通过composer包含它。
这是一个快速且非常脏的实现。
<强>的.htaccess 强>
RewriteEngine On
RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
<强>的index.php 强>
<?php
//This is the file that sets timezones and loads the Aura Router
require(__DIR__ .'/vendor/aura/router/bootstrap.php');
//Create the router
use Aura\Router\RouterContainer;
$routerContainer = new RouterContainer();
//This object will let us define routes
$map = $routerContainer->getMap();
/*
* This could be any of the following requests:
*
* $map->get()
* $map->post()
* $map->patch()
* $map->delete()
* $map->options()
* $map->head()
*/
$map->get('post', '/blog/{id}', function ($request, $response) {
$id = (int) $request->getAttribute('id');
$response->body()->write("You asked for blog entry {$id}.");
return $response;
});
以下是路由器功能的一小部分示例:
//Regex validate a url parameter
$map->get('post', '/blog/{id}', function($request, $response) {...})->tokens(['id' => '\d+'])
//Define the same values as in your URL, and you can use the array values to hold your url parameters. This is useful if you have optional parameters, and want to set your default values for routing in your router, rather than in your models
->values(['id' => null])
//Require request on HTTPS port 443
->secure()
//Set authorization level for a route (very flexible - you could even use bitmasking if you felt up for it)
->auth(['isAdmin' => true])
我的路由分为子类,我将地图中的参数抽象为单独的数组,然后将数据传递给Aura View库,以获得非常纯粹的MVC结构。
Here是路由器的完整文档。