.htaccess redirect if ANY query string is present?

2020-03-28 17:23发布

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

4条回答
ら.Afraid
2楼-- · 2020-03-28 17:52

I wasn't sure from your description exactly what you were after, so you should keep or remove the second RewriteCond depending on your specific needs.

RewriteEngine On

# Remove the query string with a redirect
RewriteCond %{QUERY_SRING} !=""
# But only if the path ended with a /
# (remove this to redirect anything with a query string)
RewriteCond %{REQUEST_URI} /$
RewriteRule .* /$0? [R=301,L]

# Then perform your internal rewrite
RewriteCond $1 !^(index\.php|media|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
查看更多
太酷不给撩
3楼-- · 2020-03-28 17:52

Possibly because you have QSA added on there - it means Query String Append.

查看更多
可以哭但决不认输i
4楼-- · 2020-03-28 18:00

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]
查看更多
劫难
5楼-- · 2020-03-28 18:04
RewriteCond %{REQUEST_URI} ^(.*/)([^/]+)$
RewriteRule . %1 [R=301,L]
查看更多
登录 后发表回答