簡體   English   中英

如何更正重定向到HTML頁面中的任何URL?

[英]How to correct redirect to any url from within a HTML page?

當然,我知道如何從HTML內重定向到任何網址,例如

<meta http-equiv="refresh" content="0; url=http://example.com/" />

要么

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1; url=http://example.com">
        <script type="text/javascript">
            window.location.href = "http://example.com"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow this <a href='http://example.com'>link to example</a>.
    </body>
</html>

但是我想知道的是,這是嘗試像bit.ly那樣進行重定向時如何做到的正確方法。 我提問的目的是什么?

我為多個服務(例如Skype for Business Meeting,GoToMeeting,HRS酒店預訂,其他預訂頁面等)提供了許多靜態URL。其中所有都在Url中包含任何ID /標記,需要正確鍵入才能獲得正確的服務。 例如,在Skype會議上,我將此URL提供給我的同事/客戶。 我想要做的是在域上添加一些static.html

www.my.dom/skype

並將這個網址提供給我的客戶,以便於記住和輸入。

因此,我的問題是:

這是擁有靜態重定向URL的正確方法,也許不會被AntiVirus軟件,瀏覽器安全檢查等阻止。原因我不希望用戶在重定向期間收到任何錯誤消息。

通過httpd.conf啟用mod_rewrite和.htaccess,然后將此代碼放在DOCUMENT_ROOT目錄下的.htaccess中:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (^|&)title=abc(&|$) [NC]
RewriteRule ^a/b/(.*)$ http://www.example.com/xyz/? [L,R=302,NC]

請記住,RewriteRule與斜杠不匹配。

確認其工作正常后,將R = 302替換為R = 301。 在測試mod_rewrite規則時,避免使用R = 301(永久重定向)。

您還可以使用303狀態代碼。 優點是它也適用於POST請求:

https://en.wikipedia.org/wiki/HTTP_303

請求:

POST / HTTP/1.1
Host: www.example.com

響應:

HTTP/1.1 303 See Other
Location: http://example.org/other

關於“正確的方法是什么”:任何適合您需求的方法。 HTML標記/屬性的存在表明它是正確的,服務器響應代碼也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM