Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File htaccess目录、文件和变量_File_.htaccess_Variables_Directory - Fatal编程技术网

File htaccess目录、文件和变量

File htaccess目录、文件和变量,file,.htaccess,variables,directory,File,.htaccess,Variables,Directory,我正在尝试获得类似于$\u SERVER[“PATH\u INFO”]的东西,但奇怪的服务器问题阻止我使用它 在我的应用程序中,链接可以是 www.domain.com/folder/file/variable www.domain.com/folder/file www.domain.com/file/variable or www.domain.com/file/ 使用.htaccess,我试图访问正确的页面,而不是重定向到index.php或类似的页面 到目前为止,我有这个,但它不起作用

我正在尝试获得类似于$\u SERVER[“PATH\u INFO”]的东西,但奇怪的服务器问题阻止我使用它

在我的应用程序中,链接可以是

www.domain.com/folder/file/variable
www.domain.com/folder/file
www.domain.com/file/variable or
www.domain.com/file/
使用.htaccess,我试图访问正确的页面,而不是重定向到index.php或类似的页面

到目前为止,我有这个,但它不起作用:)

我确信我需要使用RewriteCond%{REQUEST_FILENAME}-f来检查请求是文件名还是目录。。。但我无法让它工作

变量可以包含所有奇怪的字符-这就是为什么我匹配点。。。也许我应该尝试将文件/文件夹名仅与a-z匹配(因为我认为它们只包含a-z、u或-)


非常感谢您提供的任何帮助,因为这已经是近两天的痛苦:)

将最具体的重写规则颠倒过来

RewriteEngine On
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2.php?x=$3 [L]

# RewriteRule to check that the file is exists here
RewriteCond %{REQUEST_URI} ^(.+)/(.+)$
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}.php -f
RewriteRule ^(.+)/(.+)$ /$1/$2.php [L]

# If file is not exists, then check by put to the variable
RewriteRule ^(.+)/(.+)$ /$1.php?x=$2 [L]
RewriteRule ^(.+)$ /$1.php [L]

大多数人/框架将没有指定扩展的所有内容传递给一个php前端控制器,然后该控制器会确定要做什么。我认为大多数人走这条路的一个原因就是因为mod_重写是多么复杂

多亏@LazyOne的提示,我才解决了这个问题

htaccess文件现在看起来如下所示:

RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ /$1/$2.php?x=$3
RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)/$ /$1/$2.php
RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ /$1.php?x=$2
RewriteRule ^([-a-zA-Z0-9_]+)/$ /$1.php

所有文件夹或文件路径必须以“/”结尾,而变量不能以“/”结尾。这在我的框架中不是问题,但可能对其他人没有用处。

是的,是的,很抱歉没有详细说明,一切都在那里,请重新编写引擎,等等。。。我的htaccess配置基本上可以正常工作,但当时只适用于一种情况。如果我只注释一行,并点击相应的URL,它会起作用。1)因为您使用的模式首先从较长的模式开始,因为
^(.+)$
将匹配
/file/
以及
/file/variable
以及
/folder/file/variable
;2) 使用
([^/]+)
而不是
^(.+)$
——这将有助于
RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ /$1/$2.php?x=$3
RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)/$ /$1/$2.php
RewriteRule ^([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ /$1.php?x=$2
RewriteRule ^([-a-zA-Z0-9_]+)/$ /$1.php