[英]Redirect subfolder to the main domain (root) using mod_rewrite (htaccess)
因此,我从另一个人那里购买了一个脚本(一点都不花哨,让我们说它只是一个定制的CMS),该脚本有点复杂,并且使用的框架除了“ bootstrap”,“ laravel”之类的名称外我一无所知。 我是一位了解基础知识的人,并且从中级水平上了解了一些事情,但是只有一些,所以当我看到一些不错的解决方案时,我会感到困惑。
像这里一样-我从未见过以奇怪的扩展名和两个 .htaccess文件为基础构建的页面-一个在根目录下,第二个在/ public /文件夹中。 尽管如此,即使文件太多,也可以正常运行。
这是问题的描述:
因此看来,当浏览器加载页面(domain.com)时,它请求(我猜)来自/ public /文件夹的内容,并且一切正常,并且domain仍保留为domain.com。 事实是,domain.com / public也可以工作,我想在此特定地址上创建重定向,只是为了防止索引这个看起来很烂的domain.com/public地址,但请记住,domain.com应该仍然可以正常工作。
我尝试了在SO和其他页面上找到的maaany解决方案,但是它们导致页面崩溃(内部服务器错误)或什么都不做。 我认为其中一些可能有效,但仅当文件未嵌入另一个子文件夹中时才有效。 嗯,我不知道,我没主意了。 你能帮我么?
这是根目录的.httaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
这是public / .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
在我看来,您应该执行的操作,特别是根.htaccess
的第一个条件+规则旨在执行您要执行的操作。 我会稍微抛光一下:
在/.htaccess
:
RewriteEngine On
RewriteBase /
# Fix URLs that begin with "public" subdirectory
RewriteCond %{THE_REQUEST} \s/public(?:/(\S*))?\s
RewriteRule ^ %1 [NS,NE,R=301,END]
# Internally rewrite everything else
RewriteRule ^(?!public/).* public/$0 [DPI,L]
在/public/.htaccess
:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Pretend this is the root
RewriteBase /
# Redirect trailing slashes if not a folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [DPI,R=301,END]
# Handle front controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!index\.php$). index.php [DPI,END]
</IfModule>
如果您的Apache版本较旧,则可以使用L
代替END
,因为会出现500错误,所以您会知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.