Apache URL 重写无法正常工作

2023-09-24php开发问题
1

本文介绍了Apache URL 重写无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 apache-rewrite 规则来转换以下 URL:

I am trying to use apache-rewrite rule to convert the below URL:

http://localhost/foo/bar/news.php?id=24

变成这样的格式:

http://localhost/foo/bar/news/foo-bar

数字 24 是 MySQL 表中随机行的 id,其中还包含 titlecontent 字段.

The number 24 is an id of a random row from a MySQL table, which also contains title and content fields.

MariaDB [blog]> select * from articles;
+----+---------+----------+ 
| id | title   | content  |
+----+---------+----------+ 
|  1 | foo-bar | bla bla  | 
+----+---------+----------+ 
1 row in set (0.00 sec)

我的 .htaccess 中有以下规则:

I have the following rule inside my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
^news/([A_Za_z0_9_]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]

我也有一个 php 代码可以生成这样的链接:

I also have a php code that generates a link like this:

$link = "<a href='news.php?id={$row['id']}'></a>";
echo $link;  

但是,我无法获得将路径更改为所需最终结果的重写规则.

However, I can't get the rewrite rule to change the path as the desired end result.

推荐答案

首先,您需要更改 html 中的 href,以提供新的 url 格式

First of all, you would need to change the href in the html, to give the new url format

function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5  ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) 
{   
  echo "<a href="/news/$row[id]">  ". substr($row['title'], 0,26)."...</a><br/>".; }
}

将生成像 http://localhost/news/24

请注意,我从 url 中删除了 /DIRECTORY/AID,因为 htaccess 建议您希望它是 url,而不是您在文本中陈述的内容.

Note that I removed the /DIRECTORY/AID from the url, as the htaccess suggest you want that to be url, as opposed to what you stated in the text.

但是现在可以访问 http://localhost/news/this_is_article_title 类型的 url.因为 this_is_article_title 和 id 24 之间没有相关性,实现这一点的唯一方法是将 id 也添加到 url,或者让 php 查找数据库中具有此标题的新闻文章.

But now the get to the http://localhost/news/this_is_article_title type of url. Because there is no correlation between this_is_article_title and the id 24, the only way to achieve this is by either adding the id to the url too, or to have the php lookup the news-article with this title in the database.

然而,这最后一个解决方案有一些问题,因为你不能只给我们一个 URL 中的标题.你必须转义字符.此外,您还必须为数据库中的标题行添加索引以获得更好的性能.

This last solution however has some problems, as the you can't just us the title in a url. You have to escape characters. Also you'll have to add a index for the title row in the DB for better performance.

所以我将采用第一个解决方案.我们将生成这样的网址

So I'll go with the first solution. We will generate urls like this

http://localhost/news/24/this_is_article_title

首先是php部分

function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5  ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) 
{ 
  $url = "/news/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '_', $row['title']);  
  echo "<a href="$url">  ". substr($row['title'], 0,26)."...</a><br/>".; }
}

接下来是 htaccess 部分.

Next comes the htaccess part.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]

我认为应该可以.

这篇关于Apache URL 重写无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17