Question

I hope you can help me with this 301 issue.

Introduction

I've just published a re-design of a website with new clean urls. The old webpage had urls like this: www.domain.dk/Default.aspx?ID=66. And the new website urls look like this: www.domain.com/contact

So I wan't to redirect all these old urls to the new ones, and therefor i'm not keeping the old urls and no general rule can be applied.

That's just simple 301 redirects, but at the same time the old domain points to a new domain, and this is where things get dirty, I think. The old domain was www.domain.dk, but i wan't to 301 all traffic to the new domain www.domain.com and at the same time I wan't to make all the individual 301 redirects.

The problem

When I click on the link www.domain.dk/Default.aspx?ID=66 in Google I get this URL in my browser: www.domain.comindex.php/?ID=66.

On other links I get www.domain.comdefault.aspx/?ID=2

So the redirecting to the new domain works fine? But the individual redirects doesn't apply at all.

The code

This code is pasted as is from my .htaccess file on the server running apache.

The first bit is auto-generated by Concrete5 CMS to make pretty URLs.

# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --

This is the code I found to 301 redirect all traffic to urls that is not using www.esvagt.com to www.esvagt.com

## --- 301 Redirects --- ##

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]

This is just one of the manual 301 redirects.

## General - Redirects ##

redirect 301 /Default.aspx?ID=66 http://www.domain.com/contact/contact-us

Thanks in advance. If you need more information I'll gladly provide that. I have zero knowledge about writing code in .htaccess, so I'm pretty clueless. I hope you can help. :)

Was it helpful?

Solution

  1. Avoid mixing mod_rewrite and mod_alias rules.
  2. Ordering of rules is also very important so have 301 rules first and then have your catch all controller rule

Use this code for 301 redirect:

## --- 301 Redirects --- ##

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.dk$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,L]

## General - Redirects ##

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Default\.aspx\?ID=66[&\s] [NC]
RewriteRule ^ http://www.domain.com/contact/contact-us? [R=301,L]

# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --

OTHER TIPS

RewriteRule www.domain.dk/Default.aspx?ID=66 domain.com/contact/contact-us [R=301,L]

If you want to redirect all posts automatically:

RewriteRule /Artical.aspx?ID=(.+?) domain.com/article-title-$1.html [R=301,L]
or
RewriteRule /Artical.aspx?ID=(.+?) domain.com/Post.aspx?ID=$1 [R=301,L]

Then the dynamic url Artical.aspx?ID=20 will be redirect to http://www.domain.com/article-title-30.html, I successfully apply this method for my blog http://downloadapp.info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top