使用Htaccess删除文件夹名称

时间:2014-09-24 06:39:31

标签: php regex apache .htaccess mod-rewrite

我认为我的htaccess代码存在一些问题,但我无法弄明白。

我需要的是什么。

1)从地址中删除.php扩展名,例如www.domain.com/index而不是www.domain.com/index.php

2)删除文件夹名称。我的网站文件存储在名为public的文件夹中。我想删除它。 www.domain.com/public/index.php至www.domain.com/index

我写了下面的代码,请告诉我这里有什么问题。

# This will hide the folder name public and the php text
Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+public/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
# if you face any problem than try to uncomment the below line and delete the line after it
RewriteRule (?!^public/)^(.*)$ public/$1 [L,NC]

1 个答案:

答案 0 :(得分:0)

您可以遵守以下规则:

# This will hide the folder name public and the php text
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+public/(\S+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
# if you face any problem than try to uncomment the below line and delete the line after it
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]