Removing trailing question mark with htaccess

前端 未结 2 1530
悲&欢浪女
悲&欢浪女 2020-12-03 20:22

Can someone help me understand this code?

# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC] 
RewriteRule .? /%{REQUEST_URI}? [R=301,L]
相关标签:
2条回答
  • 2020-12-03 20:48
    RewriteCond %{THE_REQUEST} ? HTTP [NC] 
    RewriteRule .? ^%{REQUEST_URI}? [R=301,L]
    

    Isn't going to work, because ? is a reserved character for regular expressions and you'd need to escape it along with the space. Try:

    RewriteCond %{THE_REQUEST} \?\ HTTP [NC] 
    RewriteRule ^/?(index\.cfm)? /? [R=301,L]
    

    Additionally, you want this rule under your # remove trailing index.cfm rule, and not at the very bottom.

    0 讨论(0)
  • 2020-12-03 20:55

    1) Case 1: removing question mark

    http://example.com/page/subpage/?YOURSTRING=blabla
    

    to redirect to

    http://example.com/page/subpage/
    

    then in the beggining of .htaccess, insert:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} YOURSTRING=(.*)
    RewriteRule ^(.*)$ /$1? [R=301,L]
    </IfModule>
    
    # if wordpres isnot installed in root folder, then edit the fourth line to this
    # RewriteRule ^(.*)$ /YOUR-WORDPRESS-DIRECTORY/$1? [R=301,L]
    

    2) Case 2: redirection from question mark to another link

    http://example.com/index.php?YOURSTRING=blabla&id=44
    

    to redirect to

    http://example.com/page/subpage/
    

    Use:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} YOURSTRING=blabla&id=44
    RewriteRule ^(.*)$ http://example.com/page/subpage/? [R=301,L]
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题