regex - 需要帮助重写规则以使 url 干净且 seo 友好?

标签 regex .htaccess url-rewriting seo

当前模式是 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/

相关文章:

javascript - 正则表达式替换 Javascript 中的算术运算符

.htaccess - 带有 .htaccess 的三级域名

linux - 为什么我的 .htaccess 文件在我的本地 MAMP 测试服务器上运行良好,但在我的 Linux 生产服务器上却不起作用?

php - 如何为 web.config 文件中的某些文件强制使用 http

apache - 如何根据条件重写 URL?

regex - 将字符串拆分为列

regex - 正则表达式中可选的尾部斜杠

javascript - 在 njs (javascript) 中用连字符拆分驼峰式单词的正则表达式

PHP如何获取当前重写的URL(不包括查询字符串)?

linux - htaccess 重写在 WAMP 上离线工作,但在 Linux 主机上无法在线工作