【问题标题】:.htaccess redirect something to something.html.htaccess 将某些东西重定向到 something.html
【发布时间】:2012-01-26 18:24:06
【问题描述】:

如何引流

http://www.mysite.com/something 

http://www.mysite.com/something.html

还有

http://www.mysite.com/#something 

http://www.mysite.com/something.html

谢谢。

【问题讨论】:

    标签: html apache .htaccess redirect


    【解决方案1】:

    第一个你可以使用,

    RewriteEngine On
    RewriteRule ^something((/)?)$ something.html [nc]
    

    或者更一般地说,如果您希望 http://mysite.com/X 转到 X.html

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.html$
    RewriteRule ^([^.]+)((/)?)$ $1.html [nc]
    

    http://www.mysite.com/#something 无法使用重写规则重定向,# 之后的所有内容都不会传递给服务器。您将需要使用 JavaScript 来重定向这些链接。

    【讨论】:

    • RewriteEngine On RewriteRule ^(.*)((/?))$ $1.html [nc] 这是我需要的,但不知何故它不起作用。其内部服务器错误 500...
    • @DominikLevytskyi 抱歉,我在规则前忘记了RewriteCond,因为something.html 会匹配规则并被重定向到something.html.html 等,最终会陷入无限循环。 RewriteCond 将确保不会重写任何末尾已经有 .html 的 URL。
    • 谢谢!嗯,但现在似乎它重定向了所有内容(css、图像的路径)而且我只需要重定向页面 URL :(
    • @DominikLevytskyi 再次更新。现在它只会在 URL 没有“。”时重定向。在其中,因此“blah.css”将传递到正确的文件。另一种方法是为要通过的文件添加更多的RewriteCond。
    猜你喜欢
    • 1970-01-01
    • 2012-08-24
    • 2011-10-28
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多