使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)


問題描述

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

I've been trying to rewrite a URL such as

www.somesite.com/?x=372

into a url

www.somesite.com/

My current code does not seem to work

RewriteEngine On

RewriteCond %{QUERY_STRING} x=(.*)

RewriteRule http://www.somesite.com/ [R=301,L]

I've looked up countless ways of trying to do it with htaccess and still no success.

‑‑‑‑‑

參考解法

方法 1:

If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:

RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]

You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.

In Apache 2.4 or newer you can use the QSD query string discard flag:

RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]

(by BrianJon Lin)

參考文件

  1. Remove variable from base URL with htaccess (CC BY‑SA 3.0/4.0)

#Variables #url #.htaccess






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論