htaccess的mod-rewrite可选的斜线 [英] htaccess mod rewrite with optional slash

查看:109
本文介绍了htaccess的mod-rewrite可选的斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是从一个旧网站切换到新站点的搜索引擎优化更好的URL。 我试图做到这一点:

We're switching from an old site to a new site with better URLs for SEO. I'm trying to do this:

RewriteRule ^products/boots/materialid/(.*)/colour/(.*)$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

但问题是:

1),而不是使用(。*),我可以指定这个可以有以下字符

1) Instead of using (.*), can I specify that this can have the following characters

  • 在任何字母数字字符 A-ZA-Z0-9
  • 在欧洲的任何字符,例如德国的特殊UTF8字符
  • _ - 符号
  • 也可以包含 + 符号(如黑+皮)
  • any alphanumeric character a-zA-Z0-9
  • any Euro character such as German special UTF8 characters
  • _ and - symbols
  • also can contain a + symbol (e.g. black+leather)

2)如果以上都不行,以及我对这个常识,如何更新上面,这样斜线是可选的?例如。对于上述规则,将

2) If the above won't work, and for my general knowledge on this, how do I update the above so that a trailing slash is optional? E.g. for the above rule, going to

www.mydomain.com/materialid/leather/colour/blue/

送我到

www.mydomain.com/blue//leather

额外的斜线来,因为在原来的网址蓝之后的斜线,但我需要排除这一点。

The extra slash comes because of the slash after "blue" in the original URL, but I need to exclude this.

推荐答案

试试这个:

RewriteRule ^products/boots/materialid/([^/]*)/colour/([^/]*)/?$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

[^ /] 匹配任何字符不是斜线。 / <?/ code>表示可选尾随斜线(注意,这是捕获括号外面,因此它不会被包括在重写URL)。

[^/] matches any character that is NOT a slash. /? means an optional trailing slash (note that it is outside of the capturing parentheses, so that it will not be included in the rewritten URL).

修改

根据您的评论,添加一个可选的index.php:

As per your comment, to add an optional /index.php:

RewriteRule ^products/boots/materialid/([^/]*)/colour/([^/]*)(/|/index\.php)?$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

这篇关于htaccess的mod-rewrite可选的斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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