mod_rewrite不会在XAMPP上执行特定的重定向 - 与DirectoryIndex冲突?

时间:2014-03-03 17:07:28

标签: apache .htaccess mod-rewrite xampp

我们正在尝试设置本地开发XAMPP计算机(Apache2,在Windows 8上)。

除了一种特定的指令外,所有mod_rewrite指令都可以正常工作: 我们尝试将某些目录的请求重定向到特定文件。

RewriteEngine On
RewriteRule ^/?$ add_survey.php [QSA,L]
而是提供了

index.php。 我们还尝试了^.?$^$/*,但这些都没有效果。

这虽然有效(但过于包容)

RewriteRule ^.*$ add_survey.php [QSA,L]

我检查了通常的嫌疑人(对我而言),即Options -MultiviewsOptions -Indexes,我的同事现在正在玩mod_dir,但我真的不明白为什么会这样罪魁祸首。

澄清我们的用例:
/survey/的请求应转到add_survey.php,而所有其他请求转到例如/survey/SURVEYNAME/uploadindex.php并在此上下文中/survey/SURVEYNAME/文件作为/survey/ #-> add new survey, e.g. NAME (/survey/add_survey.php) /survey/NAME/ #-> manage existing survey NAME (/survey/index.php?survey_name=NAME) /survey/NAME/results #-> view results in NAME (/survey/results.php?survey_name=NAME) 投放。

基本上所有文件都是在特定的可用调查下构建的,唯一可以在没有特定可用调查的情况下调用的文件是允许您创建新文件的文件。

{{1}}

2 个答案:

答案 0 :(得分:2)

tl; dr:添加
DirectoryIndex disabled

为方便起见,我提出了拉取请求;)https://github.com/rubenarslan/formr.org/pull/82

有点晚了,但我今天不是在XAMPP上遇到了这个问题,而是在Ubuntu 14.04.1上的Apache 2.4.7 ......

在查看Apache / rewrite的调试输出后,看起来Apache正在尝试索引文件,而不是使用正确的重写。因此,我的解决方案(或更可能的解决方法:)是禁用调查和运行目录的DirectoryIndex:

/webroot/admin/run/.htaccess

<IfModule mod_rewrite.c>
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^/?$ add_run.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([A-Za-z0-9_]+)/?$ index.php?run_name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ $2.php?run_name=$1 [QSA,L]
    # on HU subdomain
#   RewriteBase /survey/admin/run/
</IfModule>

/webroot/admin/survey/.htaccess

<IfModule mod_rewrite.c>
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^/?$ add_survey.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([A-Za-z0-9_]+)/?$ index.php?study_name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ $2.php?study_name=$1 [QSA,L]
    # on HU subdomain
#   RewriteBase /survey/admin/survey/
</IfModule>

对于那些陷入这种纠结的人并且可能有助于深入理解的人 - 调试日志:

strip per-dir prefix: /var/www/formr/webroot/admin/survey/ -> 
applying pattern '^/?$' to uri ''
rewrite '' -> 'add_survey.php'
add per-dir prefix: add_survey.php -> /var/www/formr/webroot/admin/survey/add_survey.php

看起来很好......但等等......

strip document_root prefix: /var/www/formr/webroot/admin/survey/add_survey.php -> /admin/survey/add_survey.php
internal redirect with /admin/survey/add_survey.php [INTERNAL REDIRECT]

现在让我们尝试使用index.html

strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.html -> index.html
applying pattern '^/?$' to uri 'index.html'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.html -> index.html
applying pattern '^/?([A-Za-z0-9_]+)/?$' to uri 'index.html'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.html -> index.html
applying pattern '^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$' to uri 'index.html'
pass through /var/www/formr/webroot/admin/survey/index.html

和.cgi ......一段时间没看到这个......

strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.cgi -> index.cgi
applying pattern '^/?$' to uri 'index.cgi'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.cgi -> index.cgi
applying pattern '^/?([A-Za-z0-9_]+)/?$' to uri 'index.cgi'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.cgi -> index.cgi
applying pattern '^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$' to uri 'index.cgi'
pass through /var/www/formr/webroot/admin/survey/index.cgi

和.pl ...

strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.pl -> index.pl
applying pattern '^/?$' to uri 'index.pl'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.pl -> index.pl
applying pattern '^/?([A-Za-z0-9_]+)/?$' to uri 'index.pl'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.pl -> index.pl
applying pattern '^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$' to uri 'index.pl'
pass through /var/www/formr/webroot/admin/survey/index.pl

最后但并非最不重要.php ...噢......看...... index.php ......

strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.php -> index.php
applying pattern '^/?$' to uri 'index.php'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.php -> index.php
applying pattern '^/?([A-Za-z0-9_]+)/?$' to uri 'index.php'
strip per-dir prefix: /var/www/formr/webroot/admin/survey/index.php -> index.php
applying pattern '^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$' to uri 'index.php'
pass through /var/www/formr/webroot/admin/survey/index.php

答案 1 :(得分:1)

您可以在/survey/.htaccess

中使用此功能
DirectoryIndex add_survey.php

修改

<IfModule mod_rewrite.c>
DirectoryIndex add_survey.php
RewriteEngine On
RewriteBase /survey/

RewriteRule ^/?$ add_survey.php [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(\w+)/(\w+)/?$ $2.php?study_name=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(\w+)/?$ index.php?study_name=$1 [QSA,L]

</IfModule>