如何在php中重写htaccess文件中的url?

如何在php中重写htaccess文件中的url?,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我是php新手。 下面是我的.htaccess文件 DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine On #RewriteBase / RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ RewriteRule . %1/%2 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME

我是php新手。 下面是我的.htaccess文件

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
我试着添加下面的行

RewriteRule ^.*$ index.php/qa-rewrite=$0&%{QUERY_STRING} [L]

但是
site.com/123/why do birds sing
不起作用。我应该做哪些更改才能使所需的url格式正常工作


p.S
site.com/index.php/123/why do birds sing
正在运行,但我想在没有索引的情况下重写。php

重写规则的工作方式是,它们接受参数中的任何内容,并将url重建为更可读和用户友好的url。同时将参数(秘密地)传递到页面以供使用

然后,您可以使用
$\u GET['my-parameter']

下面是您需要的重写规则

在本部分中,
^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$
将斜杠前的所有内容作为第一个参数写入,斜杠后的所有内容作为第二个参数写入。允许大写字母、小写字母、数字0-9、下划线和连字符的任意组合

RewriteRule    ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$    index.php?qa=$1&qa_1=$2    [NC,L]
然后像这样构建你的链接<代码>123/鸟儿为什么唱歌

您的url看起来像
http://example.com/123/why-do-birds-sing


快乐编码

谢谢@kuya在第一行中我有
DirectoryIndex.php
,所以这意味着它只有在url中有index.php时才会重写?行
DirectoryIndex.php
与重写无关。只有当您想使用
index.php
以外的内容作为站点上的索引页面时,才使用该行。例如(因为linux区分大小写),您可能希望在整个站点中使用Index.php作为索引页。在这种情况下,您可以使用
DirectoryIndex
进行更改。如果要使用
index.php
作为索引页,则可以删除行
DirectoryIndex.php
。因为
index.php
已经是默认的索引页面,所以它不会改变任何东西。
site.com/123/为什么小鸟唱歌
使用上述规则对我有效。你的错误是什么?“我的错误是404。当我转到
site.com/index.php/123/why Doy birds sing
时,它工作正常。通过将相同的垃圾(随机)文本放在
.htaccess
的顶部,验证您的
.htaccess
是否已启用,并查看当您在浏览器中访问页面时,它是否会生成500(内部服务器)错误?
RewriteRule    ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$    index.php?qa=$1&qa_1=$2    [NC,L]