.htaccess 正则表达式,重定向到新文件夹

发布于 2024-12-11 01:50:21 字数 891 浏览 0 评论 0原文

我正在尝试重定向一些过去遵循此模式的旧文件/文件夹:

foldername/filename.extension

文件夹名称有: (2010 或 2011)(文本 AZ az 0-9 _ 或 -)

文件名具有: (文本 AZ az 0-9 _ 或 -).extension

例如:2011aug_SomeNameHere/image.jpg

新的文件夹树按年份组织文件(因此所有内容都更深一层): 2010/文件夹名/文件名.扩展名 和 2011/foldername/filename.extension

例如:2011/2011aug_SomeNameHere/image.jpg

我的重写规则如下:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    RewriteRule ^http://(www\.)?domain.com/path/to/+(2010|2011)+([A-Za-z\_\-])+/([A-Za-z0-9\_\-]\.([A-Za-z]))$ http://domain.com/path/to/$1/$1$2/$3 [L,R=301]

</IfModule>

在 Firefox 中,我得到重定向到: domain.com/path/to/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg...

在 Chrome 中,我收到 404 错误,网址为: domain.com/path/to/2011aug_SomeNameHere/image.jpg

有人有任何想法/提示吗?

I'm trying to redirect some old files/folders that used to follow this pattern:

foldername/filename.extension

foldername has:
(2010 or 2011)(text A-Z a-z 0-9 _ or -)

filename has:
(text A-Z a-z 0-9 _ or -).extension

Ex: 2011aug_SomeNameHere/image.jpg

The new folder tree organizes the files by year (so everything is one level deeper):
2010/foldername/filename.extension
and
2011/foldername/filename.extension

Ex: 2011/2011aug_SomeNameHere/image.jpg

And I have the following for my rewriterule:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    RewriteRule ^http://(www\.)?domain.com/path/to/+(2010|2011)+([A-Za-z\_\-])+/([A-Za-z0-9\_\-]\.([A-Za-z]))$ http://domain.com/path/to/$1/$1$2/$3 [L,R=301]

</IfModule>

In Firefox I'm getting a redirect to:
domain.com/path/to/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg...

In Chrome I'm getting a 404 error with the url at:
domain.com/path/to/2011aug_SomeNameHere/image.jpg

Does anyone have any ideas/tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2024-12-18 01:50:21

尝试一下:

RewriteRule ^path/to/(2010|2011)([A-Za-z\_\-]+)/([A-Za-z0-9\_\-]+\.[A-Za-z]+)$ /path/to/$1/$1$2/$3 [L,R=301]

除非您通过一次重写处理多个域,否则不需要在重写中使用域。如果您访问前面带有 www 的域名,则您的第一个组 $1 将是您的 (www.)。我建议您查看 + 和组的使用情况以了解更多正则表达式。将 + 放在组之外可以多次匹配同一组。在您的情况下,您希望将所有内容匹配到一组中。您还为不需要的文件扩展名匹配设置了一个嵌套组。

Try this:

RewriteRule ^path/to/(2010|2011)([A-Za-z\_\-]+)/([A-Za-z0-9\_\-]+\.[A-Za-z]+)$ /path/to/$1/$1$2/$3 [L,R=301]

It is not required to use have the domain in your rewrite unless you are handling multiple domains with one rewrite. Your first group being $1 would have been your (www.) if you went to your domain with www infront. I suggest you look up your use of +'s and groups to understand more of the regex. Having your +'s outside the groups allows for matching of the same group more than once. In your case you want to match all of it in one group. You also had a nested group for your file extension match where it wasn't need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文