Need To Remove Appended %3F from URL.

sharingsunshine
sharingsunshine used Ask the Experts™
on
I need to remove an encoded ? (%3F) off of several url's.  I want to use Mod_Rewrite to accomplish that.  Here is my regex but it itsn't working.

RewriteRule ^index\.php\?controller=cms&id_cms=([0-9]+)%3F$ /index.php?controller=cms&id_cms=$1 [R=301,L]

Open in new window


This is what the original url looks like
/index.php?controller=cms&id_cms=73%3F

Open in new window


Please tell me how to get rid of the %3F appended to the url.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dr. KlahnPrincipal Software Engineer

Commented:
The regex does not match the URL.  Two reasons.

1.  The URL begins with a slash.  The regex does not.  Add a leading slash on the regex.

2.  The URL contains no underscore between "cms" and "id" -- id cms.  The regex contains an underscore -- id_cms.  Replace the underscore with a space in the regex.

Author

Commented:
I have looked at your response numerous times and I see id_cms on the regex pattern and on the url.  So to remove this misunderstanding could you please show me what you have gotten to work in a tester.

Thanks,
Dr. KlahnPrincipal Software Engineer

Commented:
On my screen there is no underscore between "id cms".

Missing space
Regarding testing the regex, as a long-time EE user you must know that testing with a regex tester is not a substitute for testing in the operational environment.  Only you can test on the actual system.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Wow, that is strange.  Here is what I posted on the original question.  https://gyazo.com/d67e931d75df84a9259e87fd48bb7b0e

You are correct about testing but at least you can get close with a tester.

This is the tester I use and I did add the slash based on your suggestion but it still doesn't match

https://gyazo.com/0cbaf16c8898b1889a4a083c1843abb7
Top Expert 2004
Commented:
Your original rule:
RewriteRule ^index\.php\?controller=cms&id_cms=([0-9]+)%3F$ /index.php?controller=cms&id_cms=$1 [R=301,L]

Open in new window


1.  As mentioned by Dr. Klahn, you may need to detect the initial '/', depending on other configuration.  An easy solution here is to make the initial slash optional.

2. The query string is not part of the test URL.  Instead, it is tested from within RewriteCond directives, using the %{QUERY_STRING} variable.

3. The percent character is considered special in mod_rewrite.  Escape it to allow for proper detection.

Altered ruleset:
RewriteCond %{QUERY_STRING} controller=cms [NC]
RewriteCond %{QUERY_STRING} id_cms=([0-9]+)\%3F [NC]
RewriteRule ^/?index\.php$ /index.php?controller=cms&id_cms=%1 [NC,R=301]

Open in new window


While that should detect the string properly, it is not very robust due to the variability of the query string.  It would be a better idea to clean the parameter once it is the application.  For example, in index.php (or an included handler), you might have something like this:
$id_cms = preg_replace('/[^0-9]/', '', $_GET['id_cms']);

Open in new window


@Dr. Klahn: I think you are having display issues in regards to the underscore.  It looks fine on my screen.

Author

Commented:
Great job, thanks so much for the help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial