【问题标题】:htaccess redirect to subfolder with a trailing slashhtaccess 重定向到带有斜杠的子文件夹
【发布时间】:2014-06-22 06:04:45
【问题描述】:

我成功地将所有请求重定向到我的网络服务器的子文件夹。

这是我正在使用的代码:

/.htaccess

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

/public/.htaccess

# redirect all urls that doesn’t have a 
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$

RewriteRule ^(.*)$ $1 [NC]

它完美地工作,所以以下重定向工作:

example.com                 -> example.com/public/
example.com/foo/            -> example.com/public/foo/
example.com/foo/about.html  -> example.com/public/foo/about.html

顺便说一下,下面的重定向有问题:

example.com/foo             -> example.com/public/foo

虽然重定向正确,但地址栏中的实际网址是:

example.com/public/foo/

所以我的问题是:

从不带正斜杠的 URL 重定向时,如何获得 干净的 URL

希望这是有道理的。

【问题讨论】:

  • /public/foo/ 是一个实际的目录; /foo/ 不是。
  • 对不起,我可能不够清楚;我只想要一个干净的网址(没有public),我不在乎斜杠。实际上,通过我的“尝试”,我尝试在实际文件夹中添加一个斜杠,希望它随后会重定向到一个干净的 url。

标签: apache .htaccess mod-rewrite redirect rewrite


【解决方案1】:

将此代码放在根 .htaccess 中:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]

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

然后只需删除/public/.htaccess,因为这不需要。

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2015-06-19
    • 2014-12-22
    相关资源
    最近更新 更多