.htaccess文件夹作为根index.php的参数 [英] .htaccess folder as parameter to root index.php

查看:46
本文介绍了.htaccess文件夹作为根index.php的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是将任何子文件夹作为参数传递给根index.php 这是代码,它实际上有效.

What I need is any subfolder to be passed as a parameter to the root index.php This is the code and it actually works.

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.+?)(/[^/]*|)$ index.php?dir=$1/ [L,QSA]

有问题:

当url是这样的时候(项目"之后没有反斜杠):
http://example.com/projects

When the url is like this (no end slash after 'projects'):
http://example.com/projects

重写规则更改地址栏中的链接,如下所示:
http://example.com/projects/?dir=projects/

the rewrite rule changes the link in the address bar and it looks like this:
http://example.com/projects/?dir=projects/

是否有可能地址栏中的url始终保持不变(无论是否有斜杠),所以dir参数对用户不可见?

Is there a chance the url in the address bar always stays the same(no matter if there is an end slash or not) so the dir parameter is not visible to the user?

我尝试了多种规则-第一个规则添加一个斜杠,然后第二个规则将目录作为参数传递,但到目前为止还算运气.

I tried with multiple rules - the first one to add an end slash, and then the second rule to pass the directory as parameter, but with no luck so far.

到目前为止,由于有了w3d,我设法使其工作了.在.htaccess中,只需添加: DirectorySlash Off

so far thanks to w3d I managed to get it working. In the .htaccess just add: DirectorySlash Off

推荐答案

tl; dr RewriteRule模式中强制使用斜杠(并删除DirectorySlash Off指令,即保持On).

tl;dr Make the trailing slash mandatory in the RewriteRule pattern (and remove the DirectorySlash Off directive, ie. keep it On).

RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]


如注释中所建议,此奇怪的"重定向是mod_dir的DirectorySlash On指令的结果,默认情况下为On.可以通过在.htaccess文件顶部添加DirectorySlash Off来快速解决此问题(或强制使用斜杠-参见上文和下文).


As suggested in comments, this "strange" redirect is the result of mod_dir's DirectorySlash On directive, which is On by default. This can be quickly resolved by including DirectorySlash Off at the top of your .htaccess file (or making the trailing slash mandatory - see above and below).

DirectorySlash On指令指示Apache自动将斜杠附加到以文件系统目录结尾的URL.从这个意义上讲,它是固定" URL. mod_dir通过301外部重定向来实现.

The DirectorySlash On directive instructs Apache to automatically append a slash to URLs that end in a file system directory. In this sense it is "fixing" the URL. mod_dir achieves this with a 301 external redirect.

因此,启用DirectorySlash时,以上内容实际上是:

So, what is actually happening in the above, when DirectorySlash is enabled, is:

  1. 初始请求:
    /projects(不带斜杠)

  1. Initial request:
    /projects (no trailing slash)

.htaccess中的内部重写:
/index.php?dir=projects/(请注意,请求URL仍为/projects)

Internal rewrite in .htaccess:
/index.php?dir=projects/ (note that the request URL is still /projects)

mod_dir现在启动,并通过在URL路径的末尾添加斜杠来修复"初始请求(/projects-> /projects/).但是,来自重写的URL(上方)的查询字符串通过以下方式传递:
/projects/?dir=projects/(这是301个外部重定向,即一个新请求!)

mod_dir now kicks in and "fixes" the initial request (/projects --> /projects/) by appending a slash to the end of the URL-path. However, the query string from the rewritten URL (above) is passed through:
/projects/?dir=projects/ (this is a 301 external redirect, ie. a new request!)

.htaccess中的内部重写(再次-新请求):
/index.php?dir=projects/&dir=projects/(请注意,请求仍然是/projects/?dir=projects/) dir=projects/查询参数的加倍是RewriteRuleQSA标志的结果(我认为其他请求是否需要该标志?).您的PHP脚本只会看到一个dir GET参数(后一个覆盖前一个参数),除非您在RewriteRule中包含了dir[]=$1/,最后将得到一个2元素数组!

Internal rewrite in .htaccess (again - new request):
/index.php?dir=projects/&dir=projects/ (note that the request is still /projects/?dir=projects/) The doubling of the dir=projects/ query param is a result of the QSA flag on the RewriteRule (which I assume is required for other requests?). Your PHP script simply sees a single dir GET param (the later overwrites the former), unless you included dir[]=$1/ in your RewriteRule and you will end up with a 2-element array!

您的RewriteRule模式看起来也不必要地复杂.您可以简单地使斜杠为可选.即:

Your RewriteRule pattern also looks unnecessarily complex. You could simply make the trailing slash optional. ie:

RewriteRule ^(.+?)/?$ index.php?dir=$1/ [L,QSA]


或者,如上所述,您可能应该离开DirectorySlash On(默认设置),然后仅使斜杠为必需!例如:


Alternatively, having said all the above, you should probably leave DirectorySlash On (default) and simply make the trailing slash mandatory! For example:

RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]

mod_dir现在将在您的内部重写开始之前 (因为如果没有尾部的斜杠,它将不匹配).这也可以更好地规范化您的URL,并且关闭URL也存在潜在的安全风险.

mod_dir will now kick in before your internal rewrite (since it won't match without a trailing slash). This is also better for canonicalising your URLs and there are also potential security risks with turning it off.

参考:
http://httpd.apache.org/docs/2.2/mod/mod_dir .html#directoryslash

这篇关于.htaccess文件夹作为根index.php的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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