Wild Card Redirect not working properly

Hi,

I’ve built a website at a new domain for a client and want to completely remove the old site so Google isn’t seeing any duplicate content and they will have a completely fresh start.

In Cpanel on the old site I’ve setup a wild card redirect for the root to point to the new website which I thought would cover things so that any page on the old site would redirect to the new domain. However, sub pages found on Google aren’t being redirected to the new site but just getting a 404 error which has baffled me.

Not great on server config stuff so any advice would be appreciated. the old site was Wordpress in case that’s relevant.

Many thanks in advance

Hi Arthur,

Are you familiar with using mod_rewrite in an .htaccess file in your old site’s web directory?

You need to have the mod_rewrite module enabled in Apache. Once mod_rewrite is working you could redirect the old to new URLS like:


RewriteEngine ON
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301]
RewriteRule ^(.*)$ http://mynewdomain.com/404.php?url=$1 [L]

This rewriting script would need to be added to a .htaccess file located in the root directory of your old domain. If you don’t know if your mod_rewrite is working then you can link to Dklynn’s tutorial as there is an example there of how to test mod_rewrite.

  • Line 1 enables the rewrite engine
  • Line 2 maps all requests on the old site and redirects it to the new domain set with a permanent redirection flag (R=301) and the so the search engines will update the robots to go to the new domain.
  • Any pages that are linked to the old site will be set in the (.*) match. The pages will then be substituted in the $1 back reference variable and so as long as the pages are named the same thing the redirection will work.
  • The final rule is reached if a page is not found. The L flag tells mod_rewrite to stop processing rules. In this example the 404.php file would handle any page request that were not found. It passes a GET parameter ‘url’ equal to the $1 variable to your php page so the value of the not found page can be reported in the 404.php page (the php variable can be accessed via $_GET[‘url’]).

If this doesn’t work then you can do a more comprehensive rewrite map to make explicit redirects. It is more complicated to set a rewrite map up and you need to have access to your http.conf or apache.conf file as this cannot be used in an .htaccess file.

Hope this helps.

Steve

Aw, Steve, really?


RewriteEngine ON
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301]
[COLOR="#FF0000"][B]# With EVERYTHING already redirected, the 404 will NEVER be seen let alone matched[/B][/COLOR]
RewriteRule ^(.*)$ http://mynewdomain.com/404.php?url=$1 [L]

Arthur,

TO redirect EVERYTHING, simply use mod_alias’ Redirect statement:

Redirect 301 / http://newdomain/

Regards,

DK

Yup I should have seen this.

Do most hosts have mod_alias enabled? He currently manages his server using cpanel and I’m not sure that it could work the way you specify?

Regards,
Steve

Thanks for the feedback guys.

I’ve edited the htaccess file to the above. The home page redirects but any URLs to sub directories or different pages generate a 404 missing page rather than redirecting to the home page on the new site which is waht I was hoping to achieve. Also I’ve uploaded a test.php page that has the phpinfo() call and it is showing a 404 on that one too. I’m baffled.

RewriteEngine ON
Redirect 301 / http://www.newdomain.co.uk/

Any ideas? Many thanks in advance again.

Steve,

mod_alias is part of Apache’s core so it is always available.

Arthur,

Hmmm, another case of failed specificity!

If you merely want to redirect everything from your old website to a specific page on the new website, try:

Redirect / http://newdomain/test.php[\\code]i.e., name the page else mod_alias should retain the {REQUEST_URI} variable.

If that fails, I fall back on mod_rewrite which would be:

RewriteEngine on
RewriteRule .? http://newdomain/test.php [R=301,L]


Regards,

DK

Thanks :slight_smile:

Thanks all- the following code in .htaccess has all indexed pages on the old site redirecting to the new home page
RewriteEngine on
RewriteRule .? http://www.newdomain.com [R=301,L]