简体   繁体   English

使用mod_rewrite(htaccess)将子文件夹重定向到主域(根)

[英]Redirect subfolder to the main domain (root) using mod_rewrite (htaccess)

So I bought a script from the other guy (nothing fancy, let's say just a customized CMS) which is kinda complicated and uses frameworks that I don't know nothing about except names like "bootstrap", "laravel" and so. 因此,我从另一个人那里购买了一个脚本(一点都不花哨,让我们说它只是一个定制的CMS),该脚本有点复杂,并且使用的框架除了“ bootstrap”,“ laravel”之类的名称外我一无所知。 I am the guy who knows the basis and know some things from intermediate level but again, only some so when I see some fancy solutions I am getting confused. 我是一位了解基础知识的人,并且从中级水平上了解了一些事情,但是只有一些,所以当我看到一些不错的解决方案时,我会感到困惑。

Like here - I have never seen page built on files with strange extensions and two .htaccess files - one in root, and second in /public/ folder. 像这里一样-我从未见过以奇怪的扩展名和两个 .htaccess文件为基础构建的页面-一个在根目录下,第二个在/ public /文件夹中。 Still, even though there are so many files, everything works really fine and fast. 尽管如此,即使文件太多,也可以正常运行。

Here is the issue's description: 这是问题的描述:

So it seems that when browser loads the page (domain.com), it requests (I guess) content from /public/ folder and everything works fine and domain remains as domain.com. 因此看来,当浏览器加载页面(domain.com)时,它请求(我猜)来自/ public /文件夹的内容,并且一切正常,并且domain仍保留为domain.com。 The thing is, that domain.com/public also works and I want to create redirection on this specific address just to prevent indexing this crappy-looking address of domain.com/public but bearing in mind, that domain.com should still work fine. 事实是,domain.com / public也可以工作,我想在此特定地址上创建重定向,只是为了防止索引这个看起来很烂的domain.com/public地址,但请记住,domain.com应该仍然可以正常工作。

I have tried maaany solutions found here on SO and on other pages but they resulted in either crashing page (internal server error) or not doing anything at all. 我尝试了在SO和其他页面上找到的maaany解决方案,但是它们导致页面崩溃(内部服务器错误)或什么都不做。 I think some of them might work but only when files are not embedded in another sub-folder. 我认为其中一些可能有效,但仅当文件未嵌入另一个子文件夹中时才有效。 Eh I don't know, I am out of ideas. 嗯,我不知道,我没主意了。 Can you please help me? 你能帮我么?

Here is the root's .httaccess 这是根目录的.httaccess

RewriteEngine On

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

And here is the public/.htaccess 这是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>

It looks to me like what you have should work, specifically the first condition + rule in the root .htaccess is meant to do what you're asking. 在我看来,您应该执行的操作,特别是根.htaccess的第一个条件+规则旨在执行您要执行的操作。 I would polish it up a little: 我会稍微抛光一下:

In /.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]

In /public/.htaccess : /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>

You can use L instead of END if your version of Apache is old, you'll know because of a 500 error. 如果您的Apache版本较旧,则可以使用L代替END ,因为会出现500错误,所以您会知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM