gpt4 book ai didi

如果存在任何查询字符串,则 .htaccess 重定向?

转载 作者:行者123 更新时间:2023-12-03 06:53:45 27 4
gpt4 key购买 nike

我目前有以下.htaccess。

<IfModule mod_suphp.c>
suPHP_ConfigPath /home/abc/php.ini
</IfModule>

RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

AuthType Basic
AuthName "Está dominado!!"
AuthUserFile "/home/abc/.htpasswds/public_html/passwd"
require valid-user

现在的问题是,如果在/之后找到任何查询字符串,我想删除该查询字符串。

所以:如果我们有:

http://www.abc.com/?somethinghere234

它应该重定向到:

http://www.abc.com/

如果我们有:

htpp://www.abc.com/something/?id212

重定向至:

htpp://www.abc.com/something/

更新:我试过这个:

尝试A)

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* /index.php/ [L,QSA]

希望 ^(.*)$ 允许查询字符串中的任何内容...

我也尝试过这个:

尝试B)

RewriteEngine on RewriteCond $1 !^(index.php|media|css|js|images|robots.txt) RewriteCond %{QUERY_STRING} ^(.)$ RewriteRule ^(.)$ http://abc.com/ [L]

这将返回 500 内部服务器错误。

尝试C)

RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
#RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://abc.com/ [R=302,L]

我承认我有点迷失在这个 .htaccess 语法中。

尝试 D)

RewriteEngine on
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteCond %{REQUEST_URI} !^/
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://abc.com/ [R=302,L]

请多多指教,内存管理

最佳答案

以下应该有效:

RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^ /index.php/? [L]
  1. 检查 QUERY_STRING 中是否有内容(您的也接受空字符串)
  2. 您的检查以防止无休止的重定向
  3. 重定向到您想要的任何位置。

来自 Apache 文档:

When you want to erase an existing query string, end the substitution string with just a question mark

编辑(再次):作为外部重定向(如果您的应用程序中根本没有任何查询字符串,您甚至可能不需要第二个 RewriteCond):

这将告诉客户端请求相同的 URI,而不需要查询字符串。

RewriteEngine on

# redirect to same URI, but without query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}? [R=301]

# CodeIgniter "magic"
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

关于如果存在任何查询字符串,则 .htaccess 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4227497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com