htaccess的重写规则的目录结构 [英] htaccess rewriterule for directory structure

查看:121
本文介绍了htaccess的重写规则的目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的.htaccess重写问题。我们要求有多个子目录和一个.html文件进入
    例如 DIR1 / file.html
    或 DIR1 / DIR2 / file.html
    或 DIR1 / DIR2 / DIR3 / file.html
    或 DIR1 / DIR2 / DIR3 / dir4 / file.html

I have the following .htaccess rewrite problem. We have requests entering with multiple subdirectories and an .html file for example dir1/file.html or dir1/dir2/file.html or dir1/dir2/dir3/file.html or dir1/dir2/dir3/dir4/file.html

我们最终需要的是一个重写规则

what we eventually need is a rewrite rule to

index.html?dir1=$1&dir2=$2&dir3=$3&dir4=$4&file=$5

(其中 DIR2 dir5 将是空的,如果路径太短)

(where dir2 to dir5 would be empty if path is too short)

有没有办法在.htaccess文件中做直接,还是有必要来处理它在PHP?

Is there any way to do that directly in the .htaccess file, or is it necessary to handle it in php?

推荐答案

我会看看我可以测试了这一点,但是这的的的问题,因为你的问题的工作指出的:

I will see if I can test this out, but this should work for the problem as your question states it:

RewriteRule     ^/([a-z0-9]+)/?([^/]*)$       /index.php?dir1=$1&$2 [QSA]
RewriteRule     ^/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$    /index.php?dir1=$1&dir2=$2&dir3=$3&dir4=$4 [L]
RewriteRule     ^/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$    /index.php?dir1=$1&dir2=$2&dir3=$3 [L]
RewriteRule     ^/([a-z0-9]+)/([a-z0-9]+)/?$    /index.php?dir1=$1&dir2=$2 [L]
RewriteRule     ^/([a-z0-9]+)/?$    /index.php?dir1=$1 [L]

强调的应该因为只有你真的可以说你的网站结构和放什么;该级联规则集将如何影响您的应用程序。

I emphasize should because only you can really say what your site structure is & how this cascading ruleset would affect your application.

使用正则表达式是相当简单:

The regex used is fairly simple:

([a-z0-9]+)

这与捕捉字母放任何目录名称;字符。如果你想捕捉,让我们说,下划线最重要的是和破折号,它会更改为类似:

That captures any directory name with letters & characters. If you want to capture—let’s say—underscores and dashes on top of that, it would change to something like:

([a-z0-9_-]+)

首先重写规则我已经设置? - ^ /([A-Z0-9] +)/([^ /] *)$ -is捕捉任何在 DIR1 这是以防万一有数据捕获这是不是一个严格的目录结构。如果你愿意,你可以发表评论了这一点。只是增加了它,因为这就是我怎么样去处理需要URL解析这样的情况。

The first rewrite rule I have set—^/([a-z0-9]+)/?([^/]*)$—is to capture anything that comes after dir1 just in case there’s data to capture that is not a strict directory structure. You can comment that out if you wish. Just added it since that’s how I like to handle situations that need URL parsing like this.

另外,你有没有考虑将其加入重写规则?也许在规则级联前顶部?

Also, have you considered adding this to the rewrite rule? Perhaps at the top before the rules cascade in?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这篇关于htaccess的重写规则的目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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