【问题标题】:what is best way to rewrite url?重写网址的最佳方法是什么?
【发布时间】:2021-04-16 02:07:23
【问题描述】:

这是我的 .htaccess 文件的代码。这是不是正确的代码?

RewriteRule ^([a-z_-]+)$ index.php?l=$1 [QSA,L]

https://www.example.com/english

RewriteRule ^([a-z_-]+)/([a-z_-]+)$ index.php?l=$1&p=$2 [QSA,L]

https://www.example.com/english/online-english-typing

RewriteRule ^([a-z_-]+)/([a-z_-]+)/([A-Za-z_-]+)$ index.php?l=$1&p=$2&t=$3 [QSA,L]

https://www.example.com/english/online-english-typing/Free-Online-Typing-in-english

这工作正常。但是还有其他方法可以重写吗?

【问题讨论】:

  • 您好 Jasveer,这取决于您的实际要求。您能否让我们知道您想从哪个 url 重写到哪个 url?并提及什么不适合您,以便更好地理解您的问题。
  • 不,先生,这工作正常。但还有其他方式重写吗?我的代码是否正确?
  • 您的规则在我看来不错,但它们非常通用,因此https://www.example.com/english 的规则也将匹配https://www.example.com/singh,所以您是想为特定的 URL 编写规则还是需要通用的规则?
  • 是的,先生,我的网址是通用的,例如:example.com/englishexample.com/punjabiexample.com/hindi

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

您的通用 URL 规则对我来说看起来不错,您可以这样设置它们,我从它们中删除了 QSA 标记。

RewriteEngine ON
##For urls like http://localhost:80/english
RewriteRule ^([a-z_-]+)$ index.php?l=$1 [L]

##For urls like http://localhost:80/english/singh
RewriteRule ^([a-z_-]+)/([a-z_-]+)$ index.php?l=$1&p=$2 [L]

##For urls like http://localhost:80/english/online-english-typing/Free-Online-Typing-in-english
RewriteRule ^([a-z_-]+)/([a-z_-]+)/([A-Za-z_-]+)$ index.php?l=$1&p=$2&t=$3 [L]

【讨论】:

  • 好的先生,我改了,你能解释一下 QSA 是什么意思
  • @JasveerSingh, The QSA flag means to append an existing query string after the URI has been rewritten. 你也可以查看这个答案stackoverflow.com/a/16468677/5866580
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多