大网数据广告活动
服务器运维问答

.htaccess can not redirect URL without extension

· 52CE 我爱测

I'm having some URLs (linked from outside) which do not have an extension, e.g www.example.com/main Originally there were files main.htm and main.html in the server's root. However, an RewriteRule ^main$ main.html [L,R=301] does …

问题原文

原帖链接 · Terminality · CC BY-SA 3.0

I'm having some URLs (linked from outside) which do not have an extension, e.g

www.example.com/main

Originally there were files main.htm and main.html in the server's root.

However, an

RewriteRule ^main$ main.html [L,R=301]

does not work.

There is no folder main/ nor a file named main in the server root.

There are other entries in the .htaccess file which do work, so I'm sure the RewriteMod is active and working. I also have rewrite rules for typos (a link to www.example.com/xyz) that can be redirected.

RewriteRule ^xyz\)$ xyz/index.html [L,R=301]

The interesting thing is that the www.example.com/main URL appears unaltered in the browser (the URL in the browser bar doesn't change (as it does with a redirect)) but the page appears with the content of the file main.de.html. If I create a file named main.da.html it appears with that content (but only after an apache restart).

I can not figure out how it gets there or how to redirect it to main.html.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Disable MutliViews at the top of your .htaccess file:

Options -MultiViews

MultiViews (part of mod_negotiation) will ordinarily search for physical files that match main (in this case) as a basename (and would respond with an appropriate mime-type). This happens before mod_rewrite. So, if you had a file called main.de.html, this will be served as an internal subrequest (no redirect) before mod_rewrite has had a chance to process.

(but only after an apache restart).

This isn't something that would ordinarily require an Apache restart. Although it's possible that there is some caching going on. (?)

RewriteRule ^main$ main.html [L,R=301]

Aside: This is unlikely to be valid as an external redirect, unless you have an existing RewriteBase directive (I assume you must have)? Otherwise you should prefix the substitution with a slash. ie. /main.html (if the file is in the document root).

www.example.com/main URL appears unaltered in the browser

If MulitViews is not the problem, then it could be that the directive is in the wrong place in the config file and other directives are perhaps conflicting. We would need to see your .htaccess file.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-12-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。