gpt4 book ai didi

regex - .htaccess 如果 URL 错误,请执行某些操作

转载 作者:行者123 更新时间:2023-12-02 00:11:50 24 4
gpt4 key购买 nike

我正在制作我的个人 CMS .我想在其中使用很酷(友好)的 URL。这是我的 .htaccess 文件代码:

RewriteEngine on
RewriteBase /
DirectoryIndex index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]

我看到在 Stack Overflow 上也使用了友好的 URL。今天我试图在 Stack Overflow 上出错。到(例如)这个 URL http://stackoverflow.com/questions/5469955/htaccess-url-rewrite-if-file-not-exists。我添加了 .php。但是我没有看到 404 错误。我们的论坛以一种神奇的方法删除了我的后记并将我重定向回正确的站点 URL。 如何在我的 CMS 中制作相同的内容

我试图将此 (RewriteRule ^([a-zA-Z-_0-9_/]+)/?$ index.php?args=$1 [L]) 行更改为 ( RewriteRule ^([a-zA-Z-_0-9_/]+)/([a-zA-Z-_0-9)$index.php?args=$1 [L]),但这没有帮助,然后我看到 500 Internal Server Error

最佳答案

如果您需要具有与 Stack Overflow 相同的行为,那么您还需要一些服务器端支持(例如 PHP、ASP 等)。考虑以下代码片段:

.htaccess:

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?id=$1&title=$2 [L,NC,QSA]

index.php:

<?php
$id = $_GET['id'];
$title = $_GET['title'];

$dbTitle = // Get title from Database query using $id
...
if ($title != $dbTitle) {
// Redirect with 301 to correct /<id>/<title> page
header ('HTTP/1.1 301 Moved Permanently');
header('Location: /' . $id . '/' . $dbTitle);
exit;
}
// The rest of your script
?>

关于regex - .htaccess 如果 URL 错误,请执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com