.htaccess RewriteRule 不起作用,需要生成一个友好的 URL [英] .htaccess RewriteRule not working, need to generate a URL friendly

查看:19
本文介绍了.htaccess RewriteRule 不起作用,需要生成一个友好的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个动态链接:

http://www.nortedigital.mx/article.php?id=36175&t=dobla_las_manos_el_snte__avala_reforma_educativa

我需要像这样以友好的 URL 进行转换:

and I need to convert in URL friendly like this:

http://www.nortedigital.mx/36174/se_enriquecio_elba_en_sexenios_del_pan.html

我有这个重写规则:

RewriteRule ^([^/]*)/([^/]*).html$ /article.php?id=$1&t=$2 [L]

但不起作用.拜托,有人可以帮我吗?

but doesn't work. Please, anybody can help me?

推荐答案

您必须在 RewriteCond 中捕获查询字符串并在 RewriteRule 中使用它

You must capture the query string in a RewriteCond and use that in the RewriteRule

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(d+)&t=(.+)$
RewriteRule ^/?article.php$ /%1/%2.html? [R,L]

这会将客户端重定向到请求,即 /36174/se_enriquecio_elba_en_sexenios_del_pan.html.现在您必须为真实页面提供服务器.为此,我们添加了一个额外的规则,类似于您在问题中已有的规则

This redirects the client to request i.e. /36174/se_enriquecio_elba_en_sexenios_del_pan.html. Now you must server the real page. For that, we add an additional rule, similar to the one you already have in your question

RewriteRule ^/?(.+?)/(.+?).html$ /article.php?id=$1&t=$2 [L]

但是现在,有一个无限的重定向循环.我们通过使用环境变量来打破这一点.这是整个完整的规则集

But now, there's an endless redirect loop. We break this by using an environment variable. Here is the whole complete ruleset

RewriteEngine On

RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteCond %{QUERY_STRING} ^id=(d+)&t=(.+)$
RewriteRule ^/?article.php$ /%1/%2.html? [R,L]

RewriteRule ^/?(.+?)/(.+?).html$ /article.php?id=$1&t=$2 [L,E=SEO:1]

只要没有设置环境变量,这个规则就会像上面那样重定向.并且服务于article.php中的真实页面,同时设置环境变量防止循环.

This rule does the redirect as above, as long as the environment variable is not set. And it serves the real page from article.php and sets the environment variable at the same time to prevent the loop.

您也可以为此目的使用 cookie.但是,如果在客户端中禁用 cookie,那将会中断.

You can use cookies for this purpose too. But that will break, if cookies are disabled in the client.

这篇关于.htaccess RewriteRule 不起作用,需要生成一个友好的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆