2013-04-03 163 views
1

我有以下.htaccess文件htaccess的重定向站点/子文件夹网站/ subfolder.php

#~ Turn on rewrite engine 
RewriteEngine on 

#~ Re-write urls for PHP documents. 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

#~ redirect site/subfolder to site/subfolder.php 
RewriteCond %{REQUESTFILENAME} !-f 
RewriteRule ^(.*)/$ $1.php 

这改变了网址,site.com/something.php到site.com/something 但最后位没有按预期工作,我有一个文件夹site.com/myfolder/,当用户转到“site.com/myfolder”时,我想显示site.com/myfolder.php而不是实际的文件夹。这实际上是现在发生的,但它踩在页面上,好像它在子文件夹中,而不是在根目录中,所以所有相关URL都被打破了,任何人都可以想到解决方案?

我的最终解决方案: [编辑]在下面发布完整的脚本。

回答

0

,使用DirectorySlash关闭后,来到了最后一个问题是,该网站是唯一进入低谷http://mysite.com/,而不是http://mysite.com

所以你需要修复与简单的错误重定向

因此,完整的解决方案是这样的:

#~ Turn off displaying index for folders and give a error 403 instead. 
Options -Indexes 

#~ Set the error page to the top folder of your website ending with a slash, use a http adres to make it act like a re-direct. 
ErrorDocument 403 http://mysite.com/ 

#~ Set default page for folders. 
DirectoryIndex index.php 

#~ Turn off directory slashes. 
DirectorySlash Off 

#~ Turn on rewrite engine 
RewriteEngine on 

#~ Re-write urls for PHP documents. 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 
0

你想/myfolder而不是/myfolder/被改写成myfolder.php?尝试使用此代码在您的.htaccess隐藏文件:

Options +FollowSymlinks 
RewriteEngine on 

RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)$ 
RewriteRule ^(.*) /%1.php 

但是,如果你想重定向并没有被改写,然后只对代码添加(空格和)[R]标志.php后。要不然?

+0

不,我只是想所有的PHP网址重写,这是不可能的PHP文件具有相同的名称作为一个文件夹导致服务器优先考虑的文件夹和忽略文件,所以它把一个尾部斜杠。 因此,唯一的解决方案是使用“DirectorySlash Off” 我现在注意到,我忘了将该行添加到我的解决方案中,我现在将其编辑以供将来参考。 – user2238464