繁体   English   中英

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

[英]How to rewrite url in htaccess file in php?

我是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>

使用以下URL格式可以正常工作

  • /?qa=123/why-do-birds-sing
  • /?qa=123&qa_1=why-do-birds-sing
  • /index.php?qa=123&qa_1=why-do-birds-sing

但我想采用以下格式

site.com/123/why-do-birds-sing 

我尝试添加以下行

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

但是site.com/123/why-do-birds-sing无法正常工作。 我应该进行哪些更改以使所需的url格式起作用?

PS site.com/index.php/123/why-do-birds-sing正在运行,但是我想在没有index.php的情况下进行重写

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

然后,您可以使用$_GET['my-parameter']正常访问接收页面上的$_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/why-do-birds-sing

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

编码愉快!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM