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.

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.