iT邦幫忙

1

如何讓多個網址(http)指向同一個網頁

jton 2023-01-16 09:33:221748 瀏覽
  • 分享至 

  • xImage

我想要 3 個或更多網址 (http) 指向同一個網頁,如何操作?
以 python 或 html、css、javascript 方式, 這是我主要學的語言

http://192.168.0.206:8080/english/index.html
http://192.168.0.206:8080/english/apple
http://192.168.0.206:8080/english?fruit_ID=001
我已經有學習了flask,可以在網址中下不同指令,但還是需要一點幫忙

是一個概念問題,所以我就沒po代碼,我只需要這個想法的key word,這樣我也可以查一下,或者幸運的剛好有人知道怎麼做,可以跟我說,謝謝

看更多先前的討論...收起先前的討論...
mathewkl iT邦高手 1 級 ‧ 2023-01-16 09:35:45 檢舉
轉址
jton iT邦新手 5 級 ‧ 2023-01-16 10:16:25 檢舉
謝謝各位的解惑,我希望用一天的時間去研究哪種方式更適配我的情況,那再進行結題,選最優解這樣,請稍微等等,如果後續也有大神想補充也非常歡迎
小哈片刻 iT邦研究生 5 級 ‧ 2023-01-16 10:30:08 檢舉
你能設定.htaccess的話,就可以設定不同的uri去讀同一個檔,兩行搞定
像下面這樣,所有url都會去讀index.php
RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]
froce iT邦大師 1 級 ‧ 2023-01-16 12:40:03 檢舉
正常部署的話是會在web server的地方做轉址,例如Nginx的設定檔。
但你要在flask讓多個route對應一個view也不是不行
在 Flask 中,您可以使用 @app.route 裝飾器來將網址映射到特定的函數。您可以將多個網址映射到同一個函數,並在函數內部判斷網址以進行適當的操作。

為了實現您的需求,您可以將多個網址映射到同一個函數,並在函數內部判斷網址以進行適當的操作。

範例:
from flask import Flask, request

app = Flask(__name__)

@app.route('/english/index.html')
@app.route('/english/apple')
@app.route('/english')
def handle_english_pages():
if request.path == '/english/index.html':
# do something
elif request.path == '/english/apple':
# do something else
elif request.path == '/english':
# do something else
else:
return "404"

if __name__ == '__main__':
app.run()

這樣, 你就可以在 handle_english_pages 這個函數內部, 根據 request.path 去判斷是哪個網址, 再進行不同的操作.
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1

關鍵字:「URL Rewriting」

1
alien663
iT邦研究生 5 級 ‧ 2023-01-16 09:59:54

google : "python flask routing"

你需要的可能是把多個routing規則都去呼叫同一個function,另外建議可以多加上一個規則在最後面,讓所有網址都指向404 not found (推http.cat),這樣網址對應不到時,就會導到404 not found的介面。

1
黃彥儒
iT邦高手 1 級 ‧ 2023-01-16 18:33:04

你用Flask的話就是這樣子

from flask import Flask

app = Flask(__name__)

@app.route("/english/apple")
@app.route("/english/index.html")
@app.route("/english")
def hello_world():
    return "<p>Hello, World!</p>"

或是

from flask import Flask

app = Flask(__name__)

@app.route("/english", defaults={'path': ''})
@app.route("/english/<path:path>")
def hello_world(path):
    return "<p>Hello, World!</p>"
0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-08 08:43:35

我想要 3 個或更多網址 (http) 指向同一個網頁,如何操作?
以 python 或 html、css、javascript 方式, 這是我主要學的語言

  • python 實現
from flask import Flask, redirect

app = Flask(__name__)

# 將http://example1.com/ 轉向到 https://ithelp.ithome.com.tw/
@app.route("/", subdomain='example1')
def example1_redirect():
    return redirect("https://ithelp.ithome.com.tw/", code=301)

# 將http://example2.com/ 轉向到 https://ithelp.ithome.com.tw/
@app.route("/", subdomain='example2')
def example2_redirect():
    return redirect("https://ithelp.ithome.com.tw/", code=301)

# 將http://example3.com/ 轉向到 https://ithelp.ithome.com.tw/
@app.route("/", subdomain='example3')
def example3_redirect():
    return redirect("https://ithelp.ithome.com.tw/", code=301)

  • javascript+html 實現
<script type="text/javascript">
  window.onload = function() {
    window.location.href = 'https://ithelp.ithome.com.tw/';
  }
</script>
  • 透過 .htaccess 檔案實現
RewriteEngine On

# 將http://example1.com/ 轉向到 https://ithelp.ithome.com.tw/
RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]

# 將http://example2.com/ 轉向到 https://ithelp.ithome.com.tw/
RewriteCond %{HTTP_HOST} ^example2.com$ [NC,OR]

# 將http://example3.com/ 轉向到 https://ithelp.ithome.com.tw/
RewriteCond %{HTTP_HOST} ^example3.com$ [NC]

# 把URL改成 https://ithelp.ithome.com.tw/
RewriteRule ^(.*)$ https://ithelp.ithome.com.tw/$1 [L,R=301]
  • PHP 實現
<?php
  header('Location: https://ithelp.ithome.com.tw/');
?>

我要發表回答

立即登入回答