使用htaccess从URL中删除公用文件夹

时间:2017-11-09 18:36:51

标签: .htaccess mod-rewrite

是否可以从网址中“删除”某个文件夹,以便有人在浏览器地址栏中键入http://www.example.com/dummy/index,我可以删除“公共”文件夹,因此网址为RewriteEngine on RewriteRule ^(.*) public/$1 [L]

我基本上想隐藏'public'文件夹,因此它永远不会出现在网址中。

我在我网站的根目录下有这个htaccess,它是www.example.com/dummy /

Options -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# Prevent people from looking directly into folders
Options -Indexes

# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

这是我公共文件夹中的htaccess,位于www.example.com/dummy/public /

RewriteRule ^(.+)public/(.*)$ index.php?url=$1$2 [QSA,L]

我尝试将最后一次RewriteRule更改为www.example.com/dummy/public/index但是当我输入|id | array | |----+---------------| |1 | [1,2,3,4] | |1 | [5,6] | |1 | [7,8] | |--------------------| 时,页面会加载,但浏览器地址栏仍会在路径中显示“public”。

是否有可能做我正在尝试的事情?

我已经看到了一些声称可以完成此操作的SO答案,例如.htaccess: remove public from URLURL-rewriting with index in a "public" folder,但它们都不适用于我。

2 个答案:

答案 0 :(得分:1)

如果您要从网址中删除public/,则规则应放在/public/.htaccess内,因为所有URI /dummy/public/开头的请求都将由/public/.htaccess内的规则管理}。

使用以下内容更改/public/.htaccess

# Prevent people from looking directly into folders
Options -Indexes -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
RewriteRule ^ /%1%2? [R=301,L,NE]

# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

答案 1 :(得分:0)

不要害怕!答案是肯定的。在.htaccess中使用以下规则从您的网址中删除/public/文件夹:

RewriteEngine On
RewriteRule ^(.*)/public /$1 [R=301,L]

这将为您提供所需的http://www.example.com/dummy/index网址,并使用301永久重定向来实现此目的。出于测试目的,我建议您将其更改为302,因为这会使其成为临时的,一旦开心,就将其更改回来。

确保在>测试之前清除缓存