htaccess的重写规则不能正常工作,需要生成一个URL友好 [英] .htaccess RewriteRule not working, need to generate a URL friendly

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

问题描述

我有这个动态链接:

<$p$p><$c$c>http://www.nortedigital.mx/article.php?id=36175&t=dobla_las_manos_el_snte__avala_reforma_educativa

和我需要在URL转换友好是这样的:

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

和我有这个重写规则:

 重写规则^([^ /] *)/([^ /] *)\。HTML $ /article.php?id=$1&t=$2 [L]
 

但不工作。请,任何人都可以帮我吗?

解决方案

您必须捕获的查询字符串中的RewriteCond和使用,在重写规则

  RewriteEngine叙述上
的RewriteCond%{QUERY_STRING} ^ n =(\ d +)及T =(。+)$
重写规则^ /?的文章\的.php $ /%1/%2.html? [R,L]
 

这会重定向客户端请求即 /36174/se_enriquecio_elba_en_sexenios_del_pan.html 。现在,您必须服务器真正的页面。为此,我们增加一个额外的规则,类似于一个你已经在你的问题

 重写规则^ /?(。+?)/(。+?)\。HTML $ /article.php?id=$1&t=$2 [L]
 

但现在,有一个无止境的重定向循环。我们打​​破这种使用环境变量。这里是整个完整的规则集

  RewriteEngine叙述上

的RewriteCond%{ENV:REDIRECT_SEO} ^ $
的RewriteCond%{QUERY_STRING} ^ n =(\ d +)及T =(。+)$
重写规则^ /?的文章\的.php $ /%1/%2.html? [R,L]

?(+)(+)。重写规则^ / / \ HTML $ /article.php?id=$1&t=$2 [L,E = SEO:1]
 

这条规则并重定向如上所述,只要环境变量未设置。它提供从 article.php 真正的网页,并设置环境变量的同时,以prevent循环。

您可以使用Cookie用于此目的了。但是,这将打破,如果Cookie在客户端禁用。

I have this dynamic link:

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

and I need to convert in URL friendly like this:

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

and i have this RewriteRule:

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

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

解决方案

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]

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]

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.

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

这篇关于htaccess的重写规则不能正常工作,需要生成一个URL友好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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