Mobile app version of vmapp.org
Login or Join
Kevin317

: How to redirect URLs with percent encoded characters and query strings? I have these URLs: 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%20bgcol

@Kevin317

Posted in: #301Redirect #Htaccess

I have these URLs:

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= 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 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= www.example.com/example.html

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

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

but it's not working.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kevin317

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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+)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme