Htaccess重定向站点/子文件夹到site/subfolder.php



我有以下。htaccess文件

#~ 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

这将url从site.com/something.php更改为site.com/something但最后一点不工作的预期,我有一个文件夹site.com/myfolder/,当用户去"site.com/myfolder"我想有site.com/myfolder.php显示而不是实际的文件夹。这实际上发生在现在,但它踩页,如果它是在子文件夹,而不是在根,所以所有的相对url被打破,谁能想到一个解决方案吗?

我的最终解决方案:

您是否希望将/myfolder而不是/myfolder/重写为myfolder.php ?尝试在.htaccess隐藏文件中使用以下代码:

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

但是如果你想重定向它而不被重写,那么只需在代码的.php之后添加(一个空格和一个) [R]标志。或者其他的吗?

使用DirectorySlash Off后出现的最后一个问题是该网站只能通过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

相关内容

最新更新