htaccess的重写URL如计算器 [英] htaccess rewrite url like stackoverflow

查看:147
本文介绍了htaccess的重写URL如计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算器产生重写URL,

所以我需要知道我可以做到像计算器?

<$p$p><$c$c>http://stackoverflow.com/questions/9168364/how-to-rewrite-seo-friendly-urls-like-stackoverflow =&GT; 200确定。(没有后面的斜杠) http://stackoverflow.com/questions/9168364/how-to-rewrite-seo-friendly-urls-like-stackoverflow/ =&GT; 200 ok了。(有斜杠) http://stackoverflow.com/questions/9168364/ =&GT; 301重定向。 http://stackoverflow.com/questions/9168364 =&GT; 301重定向。

我怎么能与mod_rewrite的做到这一点?

我有这样的事情:

 重写规则^([0-9] +)[/]([^ /] *)./cat.php?id=$1&title=$2 [L,NC ]



mydomain.com/999 =&GT; 404未找​​到。
mydomain.com/999/ =&GT; 200确定。
mydomain.com/999/test =&GT; 200确定。
mydomain.com/999/test/test2 =&GT; 200确定。
mydomain.com/999/test/test2/test3 =&GT; 200确定。
mydomain.com/999/test/test2/test3/test4 =&GT; 200确定。
 

请让我知道我该如何改变重写规则?

解决方案

您需要通过斜线任意调整你的正则表达式一点。使用这条规则:

 重写规则^([0-9] +)(:/?([^ /] *))?/ $ ./cat.php?id=$1&title = $ 2 [L,NC]
 

stackoverflow generate rewrite URLs,

so i need to know how i can do it like stackoverflow ?

http://stackoverflow.com/questions/9168364/how-to-rewrite-seo-friendly-urls-like-stackoverflow => 200 ok.(without trailing slash)
http://stackoverflow.com/questions/9168364/how-to-rewrite-seo-friendly-urls-like-stackoverflow/ => 200 ok.(with trailing slash)
http://stackoverflow.com/questions/9168364/ => 301 redirect.
http://stackoverflow.com/questions/9168364  => 301 redirect.

How i can do it with mod_rewrite ?

i have something like this:

RewriteRule ^([0-9]+)[/]([^/]*) ./cat.php?id=$1&title=$2 [L,NC]



mydomain.com/999 => 404 not found.
mydomain.com/999/ => 200 ok.
mydomain.com/999/test => 200 ok.
mydomain.com/999/test/test2 => 200 ok.
mydomain.com/999/test/test2/test3 => 200 ok.
mydomain.com/999/test/test2/test3/test4 => 200 ok.

please let me know how i change RewriteRule ?

解决方案

You need to tweak your regex a bit by making trailing slash optional. Use this rule:

RewriteRule ^([0-9]+)(?:/([^/]*))?/?$ ./cat.php?id=$1&title=$2 [L,NC]

这篇关于htaccess的重写URL如计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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