How to rewrite external links (redirect to an Exit Page) with Apache mod_rewrite or RewriteCond?

落爺英雄遲暮 提交于 2019-12-11 23:56:27

问题


I honestly spent hours trying to find a solution (and I'm normally good searching for solutions), but I seem to have an unique problem.

Let's assume that my current URL is http://www.something.com

I want to change all external links within webpages like the ones below...

1. <a href="http://other.something.com">other</a>
2. <a href="http://www.google.com">google</a>

... to this:

  1. <a href="http://www.something.com/redirect.htm?http://other.something.com">other</a>
  2. <a href="http://www.something.com/redirect.htm?http://www.google.com">google</a>

I do NOT have PHP or ASP. Let's assume that my website is static HTML only. Is there a workable Apache URL rewrite solution?

Thank you


回答1:


You can try mod_proxy_html. Quote from config guide:

Syntax: ProxyHTMLURLMap from-pattern to-pattern [flags] [cond]

This is the key directive for rewriting HTML links. When parsing a document, whenever a link target matches from-pattern, the matching portion will be rewritten to to-pattern.




回答2:


mod_substitute might be an alternative.

  • On the minus side, it is an unsophisticated regexp filter with all its drawbacks.
  • On the plus side, it is included in the default apache distribution and might be just enough to resolve your problem.

    AddOutputFilterByType SUBSTITUTE text/html
    Substitute s|<a([^>]*)href="http(.?)://(.*)"([^>]*)>|<a $1 href="http://www.something.com?redirect=http$2://$3" $4>|i
    

Edit

If your site is indeed a set of static html pages, why not do an off-line transformation of them using a proper HTML DOM tool?



来源:https://stackoverflow.com/questions/5872956/how-to-rewrite-external-links-redirect-to-an-exit-page-with-apache-mod-rewrite

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