htaccess的重定向如果有任何查询字符串是present? [英] .htaccess redirect if ANY query string is present?

查看:206
本文介绍了htaccess的重定向如果有任何查询字符串是present?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的.htaccess为present。

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

RewriteEngine叙述上
!的RewriteCond $ l ^(指数\ .PHP |媒体| CSS | JS |图片|机器人\ .TXT)
重写规则^(。*)$ /index.php/$1 [L]

与AuthType基本
AuthName指令埃斯塔dominado!
的AuthUserFile/home/abc/.htpasswds/public_html/passwd
需要有效的用户
 

现在的事情是,我想,如果有查询字符串后,发现/删除查询字符串。

所以: 如果我们有:

  

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

它应该重定向到:

  

http://www.abc.com/

如果我们有:

  

HTPP://www.abc.com/something/ ID212

跳转到:

  

HTPP://www.abc.com/something/

更新: 我试过这样的:

尝试)

 的RewriteCond%{QUERY_STRING} ^(。*)$
的RewriteCond%{REQUEST_URI}!^ / index.php文件
重写规则。* /index.php/ [L,QSA]
 

跳频的^(。*)$将允许查询字符串什么...

我也试过这样的:

TRY B)

  

RewriteEngine叙述上的RewriteCond $ 1   !^(index.php文件|媒体| CSS | JS |图片| robots.txt的)   的RewriteCond%{QUERY_STRING} ^()$   重写规则^(。的)$ http://abc.com/ [L]

这会返回一个500内部服务器错误。

TRY C)

  RewriteEngine叙述上
!的RewriteCond $ l ^(指数\ .PHP |媒体| CSS | JS |图片|机器人\ .TXT)
#RewriteCond%{REQUEST_URI}!^ / index.php文件
的RewriteCond%{QUERY_STRING} ^(。*)$
重写规则^(。*)$ http://abc.com/ [R = 302,L]
 

我承认我有点失去了对这个的.htaccess sintax。

TRY D)

  RewriteEngine叙述上
!的RewriteCond $ l ^(指数\ .PHP |媒体| CSS | JS |图片|机器人\ .TXT)
的RewriteCond%{REQUEST_URI}!^ /
的RewriteCond%{QUERY_STRING} ^(。*)$
重写规则^(。*)$ http://abc.com/ [R = 302,L]
 

请咨询, MEM

解决方案

以下应该工作:

 的RewriteCond%{QUERY_STRING}。
的RewriteCond%{REQUEST_URI}!^ / index.php文件
重写规则^ /index.php/? [L]
 

  1. 检查您是否已经得到的东西在QUERY_STRING(你也可以接受一个空字符串)
  2. 您检查prevent无尽的重定向
  3. 重定向到任何你想要的。

从Apache文档:

  

当你要删除现有的   查询字符串,结束替换   字符串只是一个问号

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

这将告诉客户端请求相同的URI没有查询字符串。

  RewriteEngine叙述上

#重定向到同一个URI,但没有查询字符串
的RewriteCond%{QUERY_STRING}。
重写规则^%{REQUEST_URI}? [R = 301]

#codeIgniter神奇
!的RewriteCond $ l ^(指数\ .PHP |媒体| CSS | JS |图片|机器人\ .TXT)
重写规则^(。*)$ /index.php/$1 [L]
 

I have the following .htaccess at present.

<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

Now the thing is, I would like to, if any query string is found after a / remove that query string.

So: If we have:

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

It should redirect to:

http://www.abc.com/

If we have:

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

Redirect to:

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

UPDATE: I've tried this:

TRY A)

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

Hopping that ^(.*)$ will allow anything on the query string...

I've also tried this:

TRY B)

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

This returns a 500 internal server error.

TRY 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]

I confess I'm kind of lost on this .htaccess sintax.

TRY 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]

Please advice, MEM

解决方案

The following should work:

RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^ /index.php/? [L]

  1. check if you've got something in QUERY_STRING (yours also accepts an empty string)
  2. your check to prevent endless redirect
  3. redirect to wherever you want.

From the Apache documentation:

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

EDIT (again): As external redirect (you may not even need the second RewriteCond if you don't have any query string at all in your application):

This will tell the client to request the same URI without query string.

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的重定向如果有任何查询字符串是present?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆