当我在子文件夹中移动文件时,htaccess代码不起作用

时间:2018-08-25 13:48:08

标签: .htaccess

我正在尝试将项目文件从根文件夹移至子文件夹。

现在所有文件都在子文件夹中。

我有以下htaccess代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /project/

ErrorDocument 404 http://localhost/404.php 
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [L,QSA] 
RewriteRule ^profile/(followers|following)/([\w-]+)/?$ $1.php?username=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

由于我将php文件移到了子文件夹(project)中,因此我必须像这样RewriteBase /那样更改RewriteBase /project/。因此,在移动文件并更改RewriteBase之后,以下代码将不起作用

RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [L,QSA]

所以我得到这个错误:

未找到 在此服务器上找不到请求的URL /profile.php。

网址是这样的:http://localhost/project/profile/john

我在这里想念的是,有人可以在这方面帮助我吗?

  

注意:我已经将htaccess文件移到了项目文件夹中。

如果我尝试打开没有伪文件夹(个人资料)的网址,如下所示:

http://localhost/project/profile?username=john,然后页面打开无误。

URL将其标准化:

http://localhost/project/profile/john

由于此规则:RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [L,QSA]

但给出未找到请求的URL /profile.php在此服务器上未找到。

1 个答案:

答案 0 :(得分:0)

@marcello-mönkemeyer的评论是正确的。您只需要这样删除斜杠:

RewriteRule ^profile/([\w-]+)/?$ profile.php?username=$1 [L,QSA]
                                ^
remove the slash / from here ---|---