删除 www 和重定向 index.html 的一些问题
我在使用以下设置执行我想做的事情时遇到困难。
我想删除所有 WWW,并将 index.html 转发到根目录。我希望这适用于所有域,所以我在 httpd.conf 目录指令中进行操作。
我尝试了很多变体但没有成功。最新版本如下(域位于 /var/www/html 内,位于单独的目录中)。
http://www.example.com/index.html > http://example.com
http://www.example.com/someother/index.html > http://example.com/someother/
谢谢,
玛丽亚
<Directory "/var/www/html/*/">
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteCond %{REQUEST_URI} /^index\.html/
RewriteRule ^(.*)index\.html$ / [R=301,L]
Options ExecCGI Includes FollowSymLinks
AllowOverride AuthConfig
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I am having trouble doing what i want to do with the following setup.
I would like to remove all WWW, and also forward index.html to root dir. I would like this to be for all domains, so i am doing inside httpd.conf directory directive.
I tried many variations with no success. Latest version is below (domains are inside /var/www/html, in seperate directories).
http://www.example.com/index.html > http://example.com
http://www.example.com/someother/index.html > http://example.com/someother/
Thanks,
Maria
<Directory "/var/www/html/*/">
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteCond %{REQUEST_URI} /^index\.html/
RewriteRule ^(.*)index\.html$ / [R=301,L]
Options ExecCGI Includes FollowSymLinks
AllowOverride AuthConfig
AllowOverride All
Order allow,deny
Allow from all
</Directory>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这些规则:
Try these rules:
这为我解决了。正如我怀疑的那样,应用重写规则的地方存在很大的差异。包括我在内的很多人似乎都没有意识到这一点。
http://wiki.apache.org/httpd/RewriteContext
Apache HTTPD 服务器处理以下请求:离散阶段。虽然这通常对用户和管理员是透明的,但当规则集放置在不同的上下文中时,它确实会对 mod_rewrite 的行为产生影响。简单地说,当规则放置在 VirtualHost 块(或主服务器上下文中)时,它们会在服务器将请求的 URI 映射到文件系统路径之前进行评估。相反,当规则放置在 .htaccess 文件中或主服务器配置中的目录块中时,将在此阶段发生后对它们进行评估。
This solves it for me. As I suspected, there is a whole lotta difference where rewriterule is applied. Many people including me seems to be unaware of this.
http://wiki.apache.org/httpd/RewriteContext
The Apache HTTPD Server deals with requests in discrete phases. While this is usually transparent to the user and administrator it does have an effect on the behaviour of mod_rewrite when rulesets are placed in different contexts. To oversimplify a little, when rules are placed in VirtualHost blocks (or in the main server context) they get evaluated before the server has yet mapped the requested URI to a filesystem path. Conversely, when rules are placed in .htaccess files, or in Directory blocks in the main server config, they are evaluated after this phase has occured.