【发布时间】:2009-11-04 23:12:30
【问题描述】:
我对以下网址使用以下 mod 重写代码:www.site.com/play/543
RewriteEngine On
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
我该如何扩展它,这样我就可以拥有几个网址,例如 www.site.com/contact 和 www.site.com/about
【问题讨论】:
标签: mod-rewrite
我对以下网址使用以下 mod 重写代码:www.site.com/play/543
RewriteEngine On
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
我该如何扩展它,这样我就可以拥有几个网址,例如 www.site.com/contact 和 www.site.com/about
【问题讨论】:
标签: mod-rewrite
RewriteRule ^(play|contact|about)/([^/]*)$ /index.php?$1=$2 [L]
可能会成功。现在对 /play/foo 的请求指向 /index.php?play=foo 和 /contact/bar 指向 /index.php?contact=bar 等等。
编辑,来自评论“虽然永远不会设置关于和联系方式。”
然后只需使用两次重写;
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
RewriteRule ^(contact|about)/?$ /index.php?a_variable_for_your_page=$1 [L]
【讨论】:
大多数 PHP 框架使用的常用方法是将用户请求的任何内容传递给 PHP 脚本(通常是资产 *.png、*.js *.css)和/或公共目录,然后解释PHP 端的路由。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d #if not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f #if not an existing file
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] #pass query string to index.php as $_GET['url']
</IfModule>
在 PHP 方面,避免这样的事情非常重要
$page = getPageFromUrl($_GET['url']);
include($page);
因此在接受用户输入和清理/过滤时要非常小心,以避免远程用户访问您的网络主机上的非公开文件。
【讨论】: