简体   繁体   English

Mod_Rewrite清除URL

[英]Mod_Rewrite to clean URLs

I'm trying to create a WordPress style URL system where I can have the user create a page, which automatically creates a "slug" version of it. 我正在尝试创建一个WordPress样式的URL系统,让用户可以创建一个页面,该页面会自动创建一个“ slug”版本。

I will be using index.php to query the database to match the slug to the page. 我将使用index.php来查询数据库以将子弹匹配到页面。

I have tried copying parts of WordPress' .htaccess, however shows a 404 error. 我尝试复制WordPress的.htaccess的一部分,但是显示404错误。

My URL format is similar to the following: 我的URL格式类似于以下内容:

www.mysite.com/about
www.mysite.com/rentals/page1

My .htaccess file looks like: 我的.htaccess文件如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Once I get the htaccess part solved, how do I the slug from the URL? 解决htaccess部分后,如何从URL中获取信息? I imagine that even though the URL would be clean it still might possible to send it through as index.php?page=[SLUG] 我想即使该URL是干净的,它仍然有可能通过index.php?page=[SLUG]

Any ideas on where to start and how to accomplish what I'm looking for? 关于从哪里开始以及如何完成我所寻找的东西的任何想法?

I'm using the following... 我正在使用以下内容...

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

I use $_SERVER['REDIRECT_URL'] to get the path/page-name. 我使用$_SERVER['REDIRECT_URL']来获取路径/页面名称。

example... index.php/about 例如... index.php / about

$request = strip_tags(trim($_SERVER['REDIRECT_URL']));
$request = rtrim($request, "/"); // remove trailing slash 
$pg = explode('/', $request);
$page = $pg['1'];
// SELECT FROM table WHERE page = $page;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM