似乎是Laravel网站上的.htaccess mod_rewrite问题 - 正确设置?

时间:2017-07-26 08:46:20

标签: apache laravel .htaccess mod-rewrite

我有一个Laravel 5.2网站。我的.htaccess文件位于公共文件夹下。我有以下.htaccess文件:

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteRule ^category/(.*)/(.*) guitar-lessons/$2/$1 [NC,R=301]

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

以前的开发者就是这样设置的。我是网站所有者,并不是一个Web开发人员,但我非常熟悉Web开发,服务器配置等。

我更改了网站上的网址结构,因此我在.htaccess文件中添加了一行:

RewriteRule ^category/(.*)/(.*) guitar-lessons/$2/$1 [NC,R=301]

因此,应将表单site.com/category/1/scales的链接重写为site.com/guitar-lessons/scales/1

我添加了重写日志记录,在localhost上测试时,我在错误日志中看到了这一点:

add per-dir prefix: guitar-lessons/scales/1 -> /Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1
    [Wed Jul 26 00:32:47.469564 2017] [rewrite:trace2] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] explicitly forcing redirect with http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1
    [Wed Jul 26 00:32:47.469568 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] add path info postfix: http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1 -> http://local$
    [Wed Jul 26 00:32:47.469572 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '^(.*)/$' to uri 'http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1/1/s$
    [Wed Jul 26 00:32:47.469578 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] add path info postfix: http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1 -> http://local$
    [Wed Jul 26 00:32:47.469582 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '^' to uri 'http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1/1/scales'
    [Wed Jul 26 00:32:47.469594 2017] [rewrite:trace2] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] rewrite 'http://localhost/Library/WebServer/Documents/mysite/public/guitar-lessons/scales/1/1/scales' -> 'index.php'
    [Wed Jul 26 00:32:47.469598 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] add per-dir prefix: index.php -> /Library/WebServer/Documents/mysite/public/index.php
    [Wed Jul 26 00:32:47.469605 2017] [rewrite:trace2] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] strip document_root prefix: /Library/WebServer/Documents/mysite/public/index.php -> /index.php
    [Wed Jul 26 00:32:47.469608 2017] [rewrite:trace1] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89f00aaa0/initial] [perdir /Library/WebServer/Documents/mysite/public/] internal redirect with /index.php [INTERNAL REDIRECT]
    [Wed Jul 26 00:32:47.472007 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] strip per-dir prefix: /Library/WebServer/Documents/mysite/public/index.php -> index.php
    [Wed Jul 26 00:32:47.472020 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '^category/(.*)/(.*)' to uri 'index.php'
    [Wed Jul 26 00:32:47.472033 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] strip per-dir prefix: /Library/WebServer/Documents/mysite/public/index.php -> index.php
    [Wed Jul 26 00:32:47.472038 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '^(.*)/$' to uri 'index.php'
    [Wed Jul 26 00:32:47.472044 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] strip per-dir prefix: /Library/WebServer/Documents/mysite/public/index.php -> index.php
    [Wed Jul 26 00:32:47.472048 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '^' to uri 'index.php'
    [Wed Jul 26 00:32:47.472060 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] strip per-dir prefix: /Library/WebServer/Documents/mysite/public/index.php -> index.php
    [Wed Jul 26 00:32:47.472064 2017] [rewrite:trace3] [pid 10084] mod_rewrite.c(477): [client ::1:58524] ::1 - - [localhost/sid#7fd89e81f6f8][rid#7fd89e87fbf0/initial/redir#1] [perdir /Library/WebServer/Documents/mysite/public/] applying pattern '.*' to uri 'index.php'

正如你所看到的,一些奇怪的事情正在发生。起初一切都很好,但请注意这一点:

add path info postfix: http://localhost/Library/WebServer/Documents/guitar-dreams/public/guitar-lessons/scales/1 -> http://localhost/Library/WebServer/Documents/guitar-dreams/public/guitar-lessons/scales/1/1/scales

此添加路径信息正在弄乱它。知道如何以最简单的方式纠正这个问题吗?我真的想把.htaccess保存在公共文件夹中而不是让它变得复杂......

谢谢!

0 个答案:

没有答案