User Tools

Site Tools


programming:web_pages

Web Pages

Having a unique URL (“canonicalization”) for each webpage is important in improving your “Pagerank”. Canonicalization is accomplished by redirecting non-standard webpages to a preferred (“standard”) webpage. There are a number of ways to redirect a webpage, but 301 (HTTP/1.1 Status Code, “Moved Permanently”) redirect is the search engine friendly method which passes the pagerank and search engine ranking status from old to a new page. Here is an examples of how 301 redirect can be implemented on LAMP (Linux/Apache/Mysql/PHP) environment.

PHP Redirect

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?> 

Apache .htaccess: non-www to www subdomain redirect

1. Make sure your httpd.conf file has the “Directory” directive with the “AllowOverride All” setting.

<Directory /path/to/your/domain/document/root>
  AllowOverride All
</directory> 

2. Enable Mod-Rewrite Module

Your apache configuration should have the mod-rewrite module enabled, which is the default. You can verify this by perusing httpd.conf file for the following line:

LoadModule rewrite_module modules/mod_rewrite.so

3. Create a .htaccess file in the document root directory of your website with the following content.

RewriteEngine On
RewriteCond %{http_host} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 

Just replace the “example.com” with the domain name you're trying to redirect.

You can permanently redirect non-www URL to the www URL with 301 redirect. For apache web server, you can create a .htaccess file on your document root folder with the following content:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] 
programming/web_pages.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki