简体   繁体   English

简单的mod_rewrite问题

[英]Simple mod_rewrite issue

I have a real weird issue 我有一个很奇怪的问题

I'm playing around with .htaccess and trying to redirect all requests to the /test/ folder's index file. 我正在玩.htaccess,并尝试将所有请求重定向到/ test /文件夹的索引文件。

My site lies in a folder /test/ in my local htdocs folder. 我的站点位于本地htdocs文件夹中的/ test /文件夹中。 No other files exist currenlty. 当前没有其他文件。

What I expect: When I visit any url, (for example /test/category/one/ ) I should be redirected to /test/index.php 我的期望:当我访问任何URL时(例如/test/category/one/ ),我应该重定向到/test/index.php

What happens I get a 404 Not Found 我收到404 Not Found 怎么办

My .htaccess looks like this: 我的.htaccess看起来像这样:

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

I have tried setting RewriteBase /test/ 我试过设置RewriteBase /test/

This is as straight forward as it gets so why isn't it working? 这是很简单的,为什么它不起作用?

I have a Wordpress site in another folder and that works flawlessly with custom rewrites. 我在另一个文件夹中有一个Wordpress网站,并且可以完美地与自定义重写一起使用。

I even copied the Wordpress' .htaccess contents to the test site's, substituting the rewrite base and last rule with /test/. 我什至将Wordpress的.htaccess内容复制到测试站点,用/ test /代替了重写基础和最后一条规则。

Wordpress' .htaccess: (which works on a seperate WP install on same server) 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

I have been struggling with this for a while now and read quite a few SO articles with no help. 我已经为此苦苦挣扎了一段时间,在没有帮助的情况下阅读了很多SO文章。

I even write a rewrite log file and now there shows nothing when I browse to the test site but a visit to the Wordpress site writes quite a few lines. 我什至写了一个重写日志文件,但是当我浏览到测试站点时却什么也没显示,但是访问Wordpress站点会写很多行。

I am running XAMPP on a Win64 machine. 我在Win64机器上运行XAMPP。

Any help would be greatly appreciated! 任何帮助将不胜感激! =) =)

Update : Also, make sure the line endings in your .htaccess file are set appropriately. 更新 :另外,请确保正确设置.htaccess文件中的行结尾。 Apache can sometimes choke on anything that doesn't include the new-line (\\n) character. Apache有时可以阻塞不包含换行符(\\ n)的任何内容。

So it looks to me like you want to (at some level) emulate what WordPress is doing. 所以在我看来,您想要(在某种程度上)模仿WordPress在做什么。 Here's how I handled this case when I was developing some software that did the same thing: 当我开发一些做同样事情的软件时,这就是我处理这种情况的方式:

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

For files that exist (ie either the -f or -d test passes), we serve them up unchanged. 对于存在的文件(即-f-d测试通过),我们将为它们提供不变的服务。 Otherwise, we redirect incoming requests to index.php. 否则,我们会将传入的请求重定向到index.php。 Note that the /test portion of the path is not included in the RewriteRule, since the RewriteBase set up where we were starting from. 请注意,路径的/test部分未包含在RewriteRule中,因为RewriteBase设置了我们的起始位置。 So, in your example, I think it would end up being: 因此,在您的示例中,我认为最终将是:

<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, I'm no .htaccess expert. FWIW,我不是.htaccess专家。 I've simply found this to work for me in the past. 过去,我只是觉得这对我有用。

Also, if you're on a shared host (like DreamHost), you may need to set up the appropriate rules for allowing default error documents. 另外,如果您使用共享主机(例如DreamHost),则可能需要设置适当的规则以允许使用默认错误文档。 Some shared web hosts serve up a single file (failed_auth.html is one example) for error cases. 一些共享的Web主机会为错误情况提供单个文件(failed_auth.html是一个示例)。 If you're not filtering out that case, you may end up with a 404. 如果您没有过滤掉这种情况,则可能会以404结尾。

This should do the trick: 这应该做的伎俩:

# 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