.htaccess如何强制/自动清除URL [英] .htaccess how to force/automatic clean URL

查看:114
本文介绍了.htaccess如何强制/自动清除URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mod_rewrite的新手,但我正尽力使用干净的URL修复我的网站.

I'm new to mod_rewrite but am trying my best to fix up my site with clean URLs.

使用RewriteRule我可以获得它,因此您可以键入一个干净的URl并转到正确的页面,但是我遇到的麻烦是,如果使用的是凌乱的" URL,则会自动重定向到干净的URL(这是归因于用户提交的链接和内容等)

Using RewriteRule I can get it so you can type in a clean URl and get to the right page, but what I'm having trouble with is automatically redirecting to the clean URL if a "messy" one is used (which is highly possible due to user submitted links and content etc)

现在,我有了在另一个.htaccess论坛上找到的这段代码,它可以在一种情况下工作,而在另一种情况下却行不通.我会解释:

Now, I have this bit of code which I found on another .htaccess forum, and it works in one situation, but not another. I'll explain:

# FORCE CLEAN URLS
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(.*)=([^\s]+) [NC]
RewriteRule ^ %1\/%2? [R=301,L]

在这样的地址上工作正常,例如: www.domain.com/index.php?cmd=login ,它会自动重定向到 www.domain.com/cmd/登录

This works fine on an address like this, for example: www.domain.com/index.php?cmd=login it automatically redirects to www.domain.com/cmd/login

但是问题出在多个查询,例如: www.domain.com/index.php?cmd=view-profile&user=bob

But the problem comes when there is more than one query, like: www.domain.com/index.php?cmd=view-profile&user=bob

当地址中最多可以包含3个或更多查询时,我不知道如何整理这种URL. 我不能完全胜任正则表达式,因此到目前为止,我修改我的代码片段的尝试均以失败告终.

I can't figure out how to make it sort out that kind of URL when there could be up to 3 or more queries in the address. I'm not fully competent with regex, so my attempts to amend the code snippet I have has failed thus far.

任何帮助将不胜感激!我希望它们是301重定向,以便无论使用哪种类型的干净URL或凌乱的URL,网站都可以正确地建立索引并符合SEO,但我愿意接受建议!

Any help would be appreciated! I would like them to be 301 redirects so that the site can get indexed properly and be SEO compliant no matter what type of clean or messy URL is used, but I'm open to suggestions!

编辑

在使用正则表达式几个小时之后,我进步了,但又陷入了困境.

After playing around with the regex for a few hours, I've progressed but got stumped again.

如果我对此表达:

index\.php\?(.*)=([^\s]+)(&(.*)=([^\s]+))?+
$1/$2/$3/$4/$5

它将匹配index.php以后的这三个URL:

It will match these 3 URLs from index.php onwards:

http://site.com/index.php?cmd=shop& cat = 78

http://site.com/index.php?cmd=shop

http://site.com/index.php? cmd = shop& cat = 78& product = 68

但是结果输出根据其不同而有所不同.这些是我的结果:

BUT the resulted output varies depending on which it is. These are my results:

http://site.com/cmd=shop&cat/78///

http://site.com/cmd/shop////

http://site.com/cmd=shop&cat; = 78& product/68///

我确定如何将其视为可选部件,以便正确分组.

I'm nit sure how to get it to treat certain parts as optional so it groups properly.

推荐答案

您需要分别处理每对参数对.您拥有的一个可用于处理一个名称/值对,然后对2和3(和4(如果需要,则为4))进行类似处理:

You'll need to deal with each number of pairs of parameters separately. The one you have can be used to handle one name/value pair, then approach it similarly for 2, and 3 (and 4 if needed):

# To handle a single name/value pair in the query string:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?([^&=]+)=([^&\ ]+)(\ |$) [NC]
RewriteRule ^ /%1\/%2? [R=301,L]

# To handle 2:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?([^&=]+)=([^&\ ]+)&([^&=]+)=([^&\ ]+)(\ |$) [NC]
RewriteRule ^ /%1\/%2/%3/%4? [R=301,L]

# To handle 3:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?([^&=]+)=([^&\ ]+)&([^&=]+)=([^&\ ]+)&([^&=]+)=([^&\ ]+)(\ |$) [NC]
RewriteRule ^ /%1\/%2/%3/%4/%5/%6? [R=301,L]

基本上,您要在检查请求结束的(\ |$)之前添加另一个&([^&=]+)=([^&\ ]+) ,然后在目标URI的末尾添加另一个/%#/%#. #是适当的增量反向引用.

Basically, you're adding another &([^&=]+)=([^&\ ]+) before the check for the end of the request, (\ |$), and adding another /%#/%# to the end of the target URI, where the #'s are appropriate incremented backreferences.

这篇关于.htaccess如何强制/自动清除URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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