Michael Giovanni Pumo Michael Giovanni Pumo - 1 year ago 82
PHP Question

WordPress - redirect with .htaccess an old .aspx link to external URL

I am taking over an old website, that had dynamic redirects by routing through a .aspx file, like so:


I would simply like to redirect this to:


Here's what I've tried so far, but the problem is, I get a Forbidden error.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# These are Wordpress rules.
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# This is the part I'm trying out.
RewriteRule ^link.aspx$ https://twitter.com/example [R=301,L]

However, all I get is the following error:

server. Apache Server at example.com Port 443 You don't have permission to access /link.aspx on this

link.aspx doesn't actually exist and my server doesn't run ASP or .NET but I was hoping this wouldn't matter since .htaccess is at the server level.

I have also tried a more exact rule, but still get the same issue:

Redirect 301 /t/link.aspx?e=2&u=0&url=http://twitter.com/example https://twitter.com/example

Any ideas? Many thanks :)

Answer Source

I realised after much Google(ing) that for URLs with query string parameters, I needed to use a special rewrite condition %{QUERY_STRING}, like so:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^e=2&u=0&url=http://twitter.com/example $
RewriteRule ^t/link\.aspx?$ https://twitter.com/example? [R=301,L]

I also append to the URL that I'm redirecting to, a question mark ?. This removes / prevents the query string from being passed along.

Hope this helps someone else out.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download