mod_rewrite不隐藏子目录

时间:2013-10-21 04:27:55

标签: mod-rewrite

在我的服务器上,我有多个域名。

RewriteEngine On
RewriteBase /

# 1. Redirect multiple domains to http://domain.de/
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*) http://domain.de/$1 [L,R,NE]

# 2. Redirect all requests to /subdirectory
RewriteRule ^$ /subdirectory [L]

2.规则工作正常,但它没有隐藏url中的子目录,也没有按预期工作:http://domain.de/content/image.png的请求返回404,因为实际文件位于{{1} }

此外,我有一些文件夹,其中的工具位于子目录http://domain.de/subdirectory/content/image.png旁边。我想确保我仍然可以访问它们。目前正在运作。

问题

如何确保/subdirectory的请求有效?

我尝试了什么

http://domain.de/content/image.png

但是这只会在apache错误日志中返回错误500并带有以下条目:`由于可能的配置错误,请求超出了10个内部重定向的限制。

修改

在Ravi Thapliyal提供的指导之后,(我猜)还有一件事:从网址中删除子目录。

RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]

这是返回的内容,但我实际上想要获取html而不是位置标题,然后当然会将我从外部重定向到子目录,然后用户可以看到该子目录。它可能与子目录中的另一个.htaccess文件有关吗?

EDIT2

似乎问题与[user@domain html]$ curl -I domain.de/ HTTP/1.1 301 Moved Permanently Date: Mon, 21 Oct 2013 12:42:22 GMT Server: Apache/2.2.22 (Ubuntu) Location: http://domain.de/subdirectory/index.php Vary: Accept-Encoding Content-Type: text/html 后面的typo3安装有关。接受的答案按预期工作。

1 个答案:

答案 0 :(得分:2)

您的第一条规则应该执行外部重定向(内部更改域名根本不重要)

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*)$ http://domain.de/$1 [R=301,L,NE]

您的第二条规则不是必需的。新规则也将涵盖根/请求。

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

RewriteCond上的两个%{REQUEST_FILENAME}会确保您可以访问-f以外的任何文件-d或目录/subdirectory

<小时/> 基本上,如果URL路径指向任何现有目录,则条件%{REQUEST_FILENAME} !-d会阻止重定向。这样可以防止将/existing-directory这样的网址重定向到/subdirectory/existing-directory

但是,这也可能会阻止根URL /请求,这就是您收到目录索引禁止错误的原因。因此,上述条件为[OR]并且%{REQUEST_URI} ^/?$允许将/重定向到/subdirectory

相关问题