【问题标题】:.htaccess parameter redirect rule.htaccess 参数重定向规则
【发布时间】:2022-01-07 15:39:24
【问题描述】:

我想通过 .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>

【问题讨论】:

    标签: php .htaccess redirect hosting


    【解决方案1】:

    您可以在 .htaccess 或 Apache conf 中使用它:

    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 - "last" 阻止其他重写规则在此匹配时运行

    此规则不会重定向和更改 URL。如果您希望用户被重定向,您可以将R=301 添加到逗号分隔的标志列表中,即。 [QSA,L,R=301]

    【讨论】:

    • 非常感谢您的回答! :> 参考您的回答,我编写了自定义 .htaccess 代码并再次编辑了问题。这行得通吗?
    • 如果您将此与其他重写规则混合使用,您只需要一个RewriteEngine on。此规则与您的前端控制器的. /index.php 规则不兼容,因此此规则需要位于它之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2010-12-22
    • 2011-01-05
    相关资源
    最近更新 更多