Forum Moderators: phranque

Message Too Old, No Replies

htaccess : php to html redirect without the original php site

Is a rewrite with a query string possible without the original php site?

         

AnyIdeaWhy2

7:48 pm on May 11, 2009 (gmt 0)

10+ Year Member



Hello,

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.

g1smd

8:28 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should have used a rewrite so that the old URLs would still be used out on the web, and the rewrite would pull the content from the new files.

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.

AnyIdeaWhy2

9:20 pm on May 11, 2009 (gmt 0)

10+ Year Member



So if I understand you right, I should have kept the old website in order to make the rewrite work ?

g1smd

10:18 pm on May 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No not at all. The rewrite engine kicks in very early on to handle URL requests, if that is what you want to do. If a URL responds with a redirect, then it cannot respond with content. It is one or the other. I was merely commenting that you may have taken a backward step with the technology used to serve the site, in dumping the database.

There's some basics you need to be clear on. This thread from yesterday is a good read:
[webmasterworld.com...] [webmasterworld.com...]

jdMorgan

1:14 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot copy and try to use code if you don't understand what it is intended to do. That is both frustrating and potentially very dangerous to your site's functionality and search rankings. The code you copied works fine, but it will not do what you need to do, because it is intended to solve a completely different problem; It is a chain-saw, but you need a lawnmower.

Translating the non-functional

 Redirect 301 /index.php?page=products http://www.example.com/EN/products.html

to working mod_rewrite code in .htaccess, the result would be:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^page=products$
RewriteRule ^index\.php$ http://www.example.com/EN/products.html? [R=301,L]

The first two lines are only required once, at the top of all other mod_rewrite directives. In fact, the first line may not be needed at all, and it may not be allowed if not needed. The only way to find out is to test. The second line will always be required.

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

AnyIdeaWhy2

10:29 pm on May 12, 2009 (gmt 0)

10+ Year Member



Thanks for answering my questions. I am still working on my gardening skills although I'll never come close to Jim Scissorhands. Studying I just learned that I am actually learning "Regular Expression vocabulary".

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.

g1smd

12:04 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The second rule should be listed first. It should be changed to also cater for requests for "/" as well as for "/index.php" here.

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.

AnyIdeaWhy2

9:14 am on May 13, 2009 (gmt 0)

10+ Year Member



Dear g1smd,

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

AnyIdeaWhy2

9:25 pm on May 19, 2009 (gmt 0)

10+ Year Member



Thanks for everything. Most of the information is indeed already available. It is sometimes hard to find. A directory with the best treads (allow rating) might be a solution to preventing you answering the same questions again and again.

jdMorgan

1:54 am on May 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

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]

The first and second rules are comparably specific (although in different ways) and are mutually-exclusive in their active states, so their order is not so critical.

Jim

[edited by: jdMorgan at 1:55 am (utc) on May 20, 2009]

AnyIdeaWhy2

12:32 pm on May 27, 2009 (gmt 0)

10+ Year Member



Thanks again