.htaccess redirect if ANY query string is present?

前端 未结 4 367
抹茶落季
抹茶落季 2021-01-15 02:44

I have the following .htaccess at present.


 suPHP_ConfigPath /home/abc/php.ini


RewriteEngine on
RewriteCond $         


        
相关标签:
4条回答
  • 2021-01-15 03:14

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

    0 讨论(0)
  • 2021-01-15 03:27
    RewriteCond %{REQUEST_URI} ^(.*/)([^/]+)$
    RewriteRule . %1 [R=301,L]
    
    0 讨论(0)
  • 2021-01-15 03:32

    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]
    
    0 讨论(0)
  • 2021-01-15 03:37

    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]
    
    0 讨论(0)
提交回复
热议问题