.htaccess重定向404错误

问题描述:

我一直试图让这个权利现在1或2个小时,寻找类似的问题,并尝试解决方案。这可能是答案真的很明显的问题之一。.htaccess重定向404错误

我试图重定向大家谁正在访问domain.com/title/ 任何domain.com/title/ anotherpage PHP脚本,但它给我一个404错误时我尝试访问该页面。

我在我的htaccess文件使用此代码:

RewriteEngine on 
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L] 

这里是我的.htaccess文件的完整副本。

ErrorDocument 404 /error/404.php 

#Prevent full listing of files 
Options -Indexes 

#Redirect plugins 
<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteRule ^/plugins/(.*)$ /foobar.php?plugin=$1 [NC,L] 
</IfModule> 

#Hide .php extension 
<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [NC,L] 
</IfModule> 

#compress 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/xml 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/x-javascript 

我已经尝试将foobar.php脚本放在根文件夹和文件夹名称插件中。

非常感谢您的帮助。

+0

你得到的错误信息是什么?一个标准的404或类似“另外,尝试使用ErrorDocument时遇到404 Not Found错误”?还要注意,如果我没有弄错,'ErrorDocument 404/error/404.php'将指向'domain.com/error/404.php'(这是你的意图吗?)。还请尝试通过(暂时)删除您的htaccess中不相关的行来找出错误。 – Spooky 2012-07-25 21:59:09

+0

@Spooky我得到一个正常的404错误。否“另外...”。 ErrorDocument的位置是打算的,并显示我的自定义错误页罚款。我也试过我的代码的第一个片段,但它仍然给我错误。 – Mastergalen 2012-07-25 22:05:46

+0

“显示我的自定义错误页面” - 然后...我想我不理解你的问题/目标是什么。如果这与'RewriteRule'有关,那么我就会退缩,因为我不知道。 – Spooky 2012-07-25 22:11:10

看起来一切都在正确的地方,但你可能想尝试在你的正则表达式中去除前导斜杠(或者至少使其可选):

RewriteRule ^/?plugins/(.*)$ /yourscript.php?plugin=$1 [L] 

在htaccess文件,阿帕奇移除了在将它传递给htaccess文件中的规则之前,该URI的前缀(前导斜杠)。


domain.com/plugins/ 任何得到了正确的重定向。不过,domain.com/ 插件也被定向到该脚本。相反,我想拥有它,使之指向到一个文件夹名称的index.php文件“插件”

然后,你需要更改上面的规则:

RewriteRule ^/?plugins/(.+)$ /yourscript.php?plugin=$1 [L] 

(注*更改为+以匹配至少1个字符)。只要index.php是一个有效的索引,你可能不需要添加任何规则,这意味着如果你只是去/plugins/ apache将自动服务于index.php文件。

+0

谢谢,这个作品很棒。 _domain.com/plugins/**whatever**_被正确重定向。但是,_domain.com/**plugins**_也被定向到脚本。相反,我想拥有它,以便它指向一个索引。PHP文件中的文件夹名称“插件”。 – Mastergalen 2012-07-26 12:23:11

+0

@mastergalen看到我上面的编辑。 – 2012-07-26 17:19:18

+0

嗯,它仍然匹配domain.com/plugins并将其重定向到yourscript.php。我删除了规则,并去了插件文件夹,它为我服务index.php,所以它是一个有效的索引,只是一个htacess规则问题。 – Mastergalen 2012-07-27 11:22:12