簡體   English   中英

.htaccess 參數重定向規則

[英].htaccess parameter redirect rule

我想通過 .htaccess 將域重定向到“function.php”

如果我想根據每個規則重定向下面的域,我應該如何編寫 .htaccess 文件?

  1. “ex.com”到“ex.com/function.php”

  2. ex.com / {3個字符到“tt”},{剩余數字到“num”}

tt 參數總是有 3 個字符。 num參數是隨機的,可以是1,也可以是2~8。

"ex.com/3Ad32" to "ex.com/function.php?tt=3Ad&num=32"
"ex.com/5eX9901" to "ex.com/function.php?tt=5eX&num=9901"
"ex.com/5Db930" to "ex.com/function.php?tt=5Db&num=930"

3.如果域有參數,它同樣重定向它。

"ex.com/3Ad32?fo=test" to "ex.com/function.php?tt=3Ad&num=32&fo=test"
"ex.com/5eX9901?fo=ops" to "ex.com/function.php?tt=5eX&num=9901&fo=ops"
"ex.com/5Db930fo=thx&te=on" to "ex.com/function.php?tt=5Db&num=930&fo=thx&te=on"

我什至不知道 .htaccess 文件是否可以剪切一些 URL(例如“tt”參數的 3 個字符)

所有目的都是因為function.php中需要參數。

<?php
$type = $_GET['tt'];
$num = $_GET['num'];
$fo = $_GET['fo'];
if(!empty($fo)){
   $fo = "n";
}

....

------新問題。

如果我有兩個域,並且只有子域“ex.com”需要工作,我應該這樣寫嗎? 只有 Maindomain 有 SSL。

-----current .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?ex.com$
RewriteRule ^/?([0-9A-Za-z]{3})([0-9]+)$ /function.php?tt=$1$num=$2 [QSA, L]

</IfModule>

您可以在 .htaccess 或 Apache 配置文件中使用它:

RewriteEngine on
RewriteRule ^/?([0-9A-Za-z]{3})([0-9]+)$ /function.php?tt=$1&num=$2 [QSA,L]

在哪里:

  • ^/? - URL 路徑以可選斜杠開頭
  • ([0-9A-Za-z]{3}) - 恰好 3 個數字或字母在括號中,將變為$1
  • ([0-9]+) - 捕獲括號中的一位或多位數字將變為$2
  • $ - 匹配 URL 路徑的結尾
  • QSA - “查詢字符串追加”,它將保留查詢字符串中已經存在的其他參數
  • L - “最后一個”,當這個匹配時阻止其他重寫規則運行

此規則不會重定向和更改 URL。 如果您希望用戶被重定向,您可以將R=301添加到逗號分隔的標志列表中,即。 [QSA,L,R=301]

暫無
暫無

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

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