Forum Moderators: phranque
I inherited a very simple static website based on a CMS system. It used a database to show 10 static pages. I changed the code from php pages to XHTML and deleted the database.
I want to redirect the old php pages to the new html ones.
Because of the query string the following does obviously not work :
.
.
Redirect 301 /index.php http://www.example.com/index.html
Redirect 301 /index.php?page=products http://www.example.com/EN/products.html
.
.
I found on your site
.
.
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.example.com/ [R=301,L]
.
.
But it didn't work
Do I need to keep the original .php version of my site in order to make this solution work ? If yes, is there any way to work around ? It could be that I am missing some fundamentals.
Thanks for helping me out.
However, going back to static pages from a database-driven site seems like a backward step to me.
If you are certain you need to have new URLs for all your pages then you will need some redirects from old to new URLs. These will use RewriteRule with [R=301,L] flags, and you will need a RewriteCond to test the QUERY_STRING values. The redirect will also need to force the canonical domain name.
There's some basics you need to be clear on. This thread from yesterday is a good read:
[webmasterworld.com...] [webmasterworld.com...]
Translating the non-functional
Redirect 301 /index.php?page=products http://www.example.com/EN/products.html
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^page=products$
RewriteRule ^index\.php$ http://www.example.com/EN/products.html? [R=301,L]
The RewriteCond examines the query string and allows the rule to be invoked only if the query string matches.
The RewriteRule looks at the requested URL-path, and will invoke a redirect only if it matches and the RewriteCond is also satisfied.
See our Apache Forum Charter [webmasterworld.com] for links to useful resources, and please do not attempt to use this code until you understand what every character in it is for.
Jim
Nothing was working and I didn't have a clue so I started testing the simplest expressions that are documented to work just to know if I wasn't missing a bigger issue (server, hosting, database,...).
Thanks to your help I managed to get :
.
.
Options +FollowSymLinks
RewriteEngine on
#
# www.example.com/index.php to www.example.com
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
#
# www.example.com/index.php?page=products to www.example.com/EN/products.html
#
RewriteCond %{QUERY_STRING} ^page=products$
RewriteRule ^index\.php$ http://www.example.com/EN/products.html? [R=301,L]
#
# example.com to www.example.com
#
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Getting the canonical version gives me a DNS-error - can't find the server
But I think this is another issue/thread.
.
.
Thanks again g1smd an Jim for your solutions.
The index redirect can be better coded using
(([^/]+/)*) (or similar) in place of (.*) on both lines. There's a better way to code the www canonicalisation too. Change the
^example\.com pattern to be !^www\.example\.com$ instead. Several recent threads show these examples, with longer answers as to why.
I understand the improvements for the index rewrite and the canonicalisation.
But what is the reason for moving my second rule
RewriteCond %{QUERY_STRING} ^page=products$
RewriteRule ^index\.php$ http://www.example.com/EN/products.html? [R=301,L]
first ?
.
.
Do you mean I should take the following possibilities into account ?:
www.example.com/index.php?page=products/
www.example.com/index.php?page=products/index.php
Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect only direct client requests for /<any-directory>/index.php to
# www.example.com/<any-directory>/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect requests for /index.php?page=products or
# /?page=products to www.example.com/EN/products.html
RewriteCond %{QUERY_STRING} ^page=products$
RewriteRule ^(index\.php)?$ http://www.example.com/EN/products.html? [R=301,L]
#
# Externally redirect requests for all non-canonical hostnames to to www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Jim
[edited by: jdMorgan at 1:55 am (utc) on May 20, 2009]