How to redirect URLs with percent encoded characters and query strings?

How to redirect URLs with percent encoded characters and query strings? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, 301-redirect, 404, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I have these URLs:




  1. http://www.example.com/example.html%20text-decoration:%20none%E2%80%9D%3Eexample.com%3C%20%3C/i%3E%3C/span%3E%3Cbr%3E%3Cbr%3E%3Cbr%3E%3Ctable%20bgcolor=

  2. http://www.example.com/examindex/?p=2343543



How can I redirect these links using 301 redirects with a .htaccess file? I want to redirect 301 link 1 to http://www.example.com/example.html I've tried:



redirect 301 /example.html%20text-decoration:%20none%E2%80%9D%3Eexample.com%3C%20%3C/i%3E%3C/span%3E%3Cbr%3E%3Cbr%3E%3Cbr%3E%3Ctable%20bgcolor= http://www.example.com/example.html


I want to redirect link 2 to http://www.example.com/examindex/. I've tried:



redirect 301 /examindex/?p=2343543 http://www.example.com/examindex/


but it's not working.


Solution :

Redirect (mod_alias) matches against the %-decoded path. Also, you can't match the query string with this directive, so it's best to use mod_rewrite for both these redirects (which also matches against the %-decoded path). It's never a good idea to mix both mod_alias and mod_rewrite directives.



At the top of .htaccess, enable the rewrite engine...



RewriteEngine On


For "link 1" I assume we don't need to match the entire mashed up link, just matching the first bit eg. <space>text-decoration should be sufficient I would have thought? Note that since we are matching against the %-decoded path, a space is literally a space (), not %20 - but a space needs to be backslash escaped in the regex ().



RewriteRule ^example.html text-decoration /examindex/ [R=301,L]


For "link 2" we need to use the RewriteCond directive in order to match the query string. The query string is removed from the URL-path before pattern matching (which also applies to a mod_alias Redirect).



RewriteCond %QUERY_STRING =p=2343543
RewriteRule ^examindex/$ /examindex/? [R=301,L]


The trailing ? on the substitution is required to remove the query string. (Or you could use the QSD flag instead on Apache 2.4+)


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect