htaccess干净的URL是执行此操作的最佳方法是什么? [英] htaccess clean URL's what's the best way to do it?

查看:52
本文介绍了htaccess干净的URL是执行此操作的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站开发Clean Url,我注意到您在Internet上发现的很多内容都是关于将Clean URL重写为服务器可以使用的URL.像这样:

I'm working on clean Url's for my website and I noticed that what you find on the internet is pretty much about rewriting your clean urls to the url that your server can work with. So something like this:

www.domain.com/profile/username --> www.domain.com/profile.php?u=username

RewriteEngine On
RewriteRule ^profile/(.*)$ /profile.php?u=$1 [L]

因此,现在我应该在我的网站内链接到cleanurl的链接.但是从直觉上来说,链接到脏" URL感觉更扎实/更好.因此,在这种情况下,您应该对以上内容使用重定向程序,并使用重定向规则将脏网址"重新发送到干净网址,以便用户始终只能在浏览器中看到干净网址.

So now with this I should link within my website to the cleanurl's. But intuitively it feels more solid/better to link to the 'dirty' url. So in that case you should use rewriterules for the above AND a redirect rule to rederict the 'dirty url' to the clean url, so the user only sees the clean url in the browser at all time.

www.domain.com/profile.php?u=username --> www.domain.com/profile/username

这是一件好事吗?网站完成了吗?反对/赞成它的理由是什么?

Is this something that is a good thing to do. Is it done by websites? What are arguments against/ in favour of it?

以及如何进行这样的重定向.诸如此类的东西似乎不起作用:

And how do you do you do a redirect like this. Something like the following doesn't seem to work:

RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]

.htaccess我还很陌生,所以我可能会要求一些明显的东西...

I'm fairly new to .htaccess so I might ask for something that's obvious...

推荐答案

RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]

将无法工作,因为您无法与RewriteRule中的查询字符串进行匹配(标志中也有杂散空间).您还希望与实际请求而不是URI匹配,因为这只会导致其他规则的重定向循环.

Isn't going to work because you can't match against the query string from a RewriteRule (you also have a stray space in your flags). You also want to be matching against the actual request and not the URI because you'd just cause a redirect loop from your other rule.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?u=([^&\ ]+)
RewriteRule ^/?profile\.php$ /profile/%1 [R=301,L]

这篇关于htaccess干净的URL是执行此操作的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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