4

Can someone help me understand this code?

# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC] 
RewriteRule .? /%{REQUEST_URI}? [R=301,L]

Basically I have a site www.example.com that is generating a link to www.example.com/index.cfm? I need it to redirect to www.example.com for SEO duplication purposes. I managed to remove the index.cfm but the ? still stays there (www.example.com/?). The trailing slash is also removed just fine if it's the last character. I found this rule online but I'm getting a "RewriteCond: bad flag delimiters" alert in apache and it doesn't do anything.

I also have some pages like www.example.com/index.cfm?term=test for searching so I just want to get rid of the trailing question mark and not when I do have a query attached to it.

The error is in the RewriteCond. I need help understanding the condition and why it doesnt work not just the answer to it.

Just in case here is the entire htaccess:

RewriteEngine On
Rewritebase /

# remove trailing index.cfm
RewriteRule ^index.cfm(\?)?$ / [R=301,L]

# SEF URLs
SetEnv SEF_REQUEST false
RewriteRule ^[a-z\d\-]+/[a-z]\d+/? /index.cfm/$0 [NC,PT,QSA,E=SEF_REQUEST:true]
RequestHeader add SEF-Request %{SEF_REQUEST}e
RewriteCond %{HTTP:SEF_REQUES} ^true$ [NC]
RewriteRule . - [L]

# Remove trailing ?
RewriteCond %{THE_REQUEST} ? HTTP [NC] 
RewriteRule .? ^%{REQUEST_URI}? [R=301,L]

NOTE: I did search online/stackoverflow before posting and did not find a solution to my problem.

EDIT: Also I noticed that my RewriteRule ^index.cfm(\?)?$ / [R=301,L] is removing the index.cfm even if it's not the last thing in the url resulting in a 404 when i try searching something (www.example.com/index.cfm?term=test) If someone could correct me and EXPLAIN that would be great. Thanks you.

EDIT2: www.example.com/index.cfm?term=test&a=dh&j=dhjsi should NOT be redirected. www.example.com/a/b/d/f/h/w/d should not be redirected. www.example.com/index.cfm? and www.example.com/index.cfm should be redirected to www.example.com.

bia.migueis
  • 1,996
  • 2
  • 15
  • 23
  • ¿When the incoming URL should be mapped to `index.cfm`? ¿Is `index.cfm` part of the URL shown in the browser's address bar? ¿Is the query of `index.cfm`, if any, also shown in the browser's address bar? If not, ¿how are the incoming URLs that should be mapped to `index.cfm`? Lot of doubts. Please take your time to update your question with some incoming and mapped URL examples for each case. Otherwise, I think it is impossible to guess what you want, for me at least. The rules alone are not of much help. They don't work and are the reason for your question. – Felipe Alameda A Jan 04 '13 at 22:33
  • updated. those are the only examples I can think of. – bia.migueis Jan 04 '13 at 22:52

2 Answers2

5
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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Now I don't get the error anymore but instead i get a www.example.com/%5e when i try to go to www.example.com/? – bia.migueis Jan 04 '13 at 23:21
  • @bia.migueis Are you sure you removed that stray `^` in the rule's target? That shouldn't be there. You have it in your question but I removed it in my answer. – Jon Lin Jan 04 '13 at 23:22
  • oh oops i didn't see that change. your code works perfectly :) Thanks so much. Do you also know how to remove the index.cfm ONLY if its the last thing in the URL? the code I have ALWAYS removes it even if it's in the middle of the URL – bia.migueis Jan 04 '13 at 23:25
  • 1
    @bia.migueis try adding a `RewriteCond %{QUERY_STRING} ^$` right above it. – Jon Lin Jan 04 '13 at 23:27
  • one last (hopefully) question... how do I remove a trailing slash? I have RewriteRule (.*)/$ /$1 [R=301,L] but then it changes www.example.com/cat/whatever into www.example.com/index.cfm?category=whatever even when there is no trailing slash – bia.migueis Jan 04 '13 at 23:32
  • 1
    @bia.migueis Make sure the redirect rule is near the top, above any sort of routing rules (SEF rules) – Jon Lin Jan 04 '13 at 23:34
  • cool thanks. i also changed it to # remove trailing slash RewriteCond %{QUERY_STRING} ^$ RewriteRule (.*)/$ /$1 [R=301,L] and it works perfectly :) – bia.migueis Jan 04 '13 at 23:45
-1

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>
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • That doesn't remove a trailing question mark - it removes the entire query string, so doesn't answer the question at all. – NickG Apr 11 '17 at 11:23