繁体   English   中英

简单的mod_rewrite问题

[英]Simple mod_rewrite issue

我有一个很奇怪的问题

我正在玩.htaccess,并尝试将所有请求重定向到/ test /文件夹的索引文件。

我的站点位于本地htdocs文件夹中的/ test /文件夹中。 当前没有其他文件。

我的期望:当我访问任何URL时(例如/test/category/one/ ),我应该重定向到/test/index.php

我收到404 Not Found 怎么办

我的.htaccess看起来像这样:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]

我试过设置RewriteBase /test/

这是很简单的,为什么它不起作用?

我在另一个文件夹中有一个Wordpress网站,并且可以完美地与自定义重写一起使用。

我什至将Wordpress的.htaccess内容复制到测试站点,用/ test /代替了重写基础和最后一条规则。

Wordpress的.htaccess :(适用于在同一服务器上的单独WP安装)

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>

# END WordPress

我已经为此苦苦挣扎了一段时间,在没有帮助的情况下阅读了很多SO文章。

我什至写了一个重写日志文件,但是当我浏览到测试站点时却什么也没显示,但是访问Wordpress站点会写很多行。

我在Win64机器上运行XAMPP。

任何帮助将不胜感激! =)

更新 :另外,请确保正确设置.htaccess文件中的行结尾。 Apache有时可以阻塞不包含换行符(\\ n)的任何内容。

所以在我看来,您想要(在某种程度上)模仿WordPress在做什么。 当我开发一些做同样事情的软件时,这就是我处理这种情况的方式:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>

对于存在的文件(即-f-d测试通过),我们将为它们提供不变的服务。 否则,我们会将传入的请求重定向到index.php。 请注意,路径的/test部分未包含在RewriteRule中,因为RewriteBase设置了我们的起始位置。 因此,在您的示例中,我认为最终将是:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>

FWIW,我不是.htaccess专家。 过去,我只是觉得这对我有用。

另外,如果您使用共享主机(例如DreamHost),则可能需要设置适当的规则以允许使用默认错误文档。 一些共享的Web主机会为错误情况提供单个文件(failed_auth.html是一个示例)。 如果您没有过滤掉这种情况,则可能会以404结尾。

这应该做的伎俩:

# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM