How to make a catch-all handler in an .htaccess file?

房东的猫 提交于 2019-12-21 22:04:10

问题


I want to create a rule at the end of an .htaccess file that catches everything that failed to match up until then.

How can I do that?

P.S. I've already tried everything :) Actually, I haven't, but it sure seems like it!

Update: Some people are replying with RewriteRule ^.*$ or an equivalent. This does not work! It will match everything including the other 'good' URLs.


回答1:


There are actually some good answers here already, but you have responded with...

But then this matches everything including the good stuff.

This is because you aren't telling mod_rewrite to stop processing on a match. To do this, use the "L" tag after each rule, which tells mod_rewrite that "If this rule is matched, stop processing any further rules".

RewriteRule ^RSS/([^/\.]+)/?$ rss.php?Page=$1 [L]

You need to put this after EACH rule. Then, when you put the catch all at the end, it will only be hit if no other rule has been matched.

NOTE: if you are ALSO serving up resources that are not rewritten, like CSS, images, javascript files - you are honestly better off not catching all as you wouldn't want to rewrite their locations.




回答2:


It sounds like you want to look at the RewriteRule directive. Quie possibly something similar to the following:

RewriteRule ^/(.+) {target}

This will match at the root URL (/) and redirect it to the {target} url.




回答3:


RewriteRule .* page.php [L] ?



来源:https://stackoverflow.com/questions/2808180/how-to-make-a-catch-all-handler-in-an-htaccess-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!