当前模式是 http://example.com/questions.php?qcid=25&name=java
我想让它成为 http://example.com/25/java
其中 qcid 必须是 [0-9] 并且 name 必须是可读和编码。如果它包含超过 1 个单词,则它们之间必须有 '-' 连字符。例如面向对象。
最佳答案
在 .htaccess 中
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([0-9]+)/([a-z0-9\-]+)(?!(.css)|(.js))$ questions.php?qcid=$1&name=$2 [NC]
在 php 中,您需要捕获名称并去除连字符并在需要时修复大写字母。例如,假设我们希望名称采用小驼峰形式:
$_GET['name'] = "this-is-a-test-name";
$name = '';
$exploded = explode('-',$_GET['name']);
foreach($exploded as $piece) {
$name .= ucfirst($piece);
}
// If you have PHP >= 5.3
// $name = lcfirst($name);
// Otherwise
$name{0} = strtolower($name{0});
echo $name;
关于regex - 需要帮助重写规则以使 url 干净且 seo 友好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13488558/