htaccess 清除 URL 的最佳方法是什么? [英] htaccess clean URL's what's the best way to do it?

查看:20
本文介绍了htaccess 清除 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.但从直觉上讲,链接到脏"网址感觉更可靠/更好.因此,在这种情况下,您应该对上述内容使用 rewriterules,并使用重定向规则将脏 url"重新定义为干净的 url,这样用户始终只能在浏览器中看到干净的 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天全站免登陆