htaccess RewriteRule重写到子文件夹

时间:2017-03-06 12:45:59

标签: apache .htaccess mod-rewrite laravel-5 url-rewriting

我正在尝试将url'test'重写为/ public中的子目录。这是一个Laravel应用程序结束,我想在Laravel的公共/测试独立程序中运行内容,因此我应该阻止通过Laravel路由器。

/ test /应该无形转发到/ public / test / whatever(网址应该保留/测试/无论如何)。

我现在有了这个:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_URI} ^/test/ [NC]
   RewriteRule ^(.*)$ public/test/$1 [L]
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

到目前为止,这是转发到:/public/test/test/(index.php)

我想我很亲密......

更新

在公共目录中也是一个.htaccess文件(原文,来自Laravel 5.4):

<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]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

1 个答案:

答案 0 :(得分:1)

您只需要最后一条规则

RewriteRule ^(.*)$ public/$1 [L]

表示根目录.htaccess文件。这会将所有内容重写为public/.htaccess,包括以/test开头的请求。

只要public/test/是目录,并且以/test/开头的任何请求的网址都是public/test内的真实文件,这就足够了,因为条件

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

将阻止index.php处理现有文件和目录。

如果test不是现有目录,或者/test/...的请求不是真实文件,则需要其他规则。可能在/public/.htaccess/public/test/.htaccess

/test重写为/public/test/test的原因是

的组合
# /.htaccess
RewriteRule ^(.*)$ public/test/$1 [L]

其中(.*)已包含test,后者变为public/test/test,然后

# /public/.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

因为public/test/test不是现有文件或目录( - &gt; RewriteCond),因此由此规则处理。

相关问题