Converting multiple dynamic pages to static
I have a PHP based site with dynamic pages , such as /page.php?ID=1. The ID in the URL represents a unique key in a MySQL database of an entry which have a "Name" field as well. I understand that in order to convert all my URLs from /page.php?ID=1 to /page/New-York (where the Name field of row ID=1 is New York) I need to use .htaccess file.
What will be the best way to do that? How can I use the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^ID=1$
RewriteRule ^/page.php$ http://www.example.com/page/New-York [L,R=301]
but in a way t开发者_Python百科hat it will retrieve the Name (in this case New York) from the MySQL database, for all entries?
Edit:
Repox's answer is very good for the next step, that is how to fetch an entry from the DB once I set up the static URL.
However, I have another problem which I need to begin with: Since my site is couple of years old, Google has already indexed all my pages, so /page.php?ID=2
and /page.php?ID=3
are already in Google's database. So I need to create a 301 Redirection (in the .htaccess) for EACH existing page to the new page, i.e:
redirect 301 /page.php?ID=1 /page/new-york
redirect 301 /page.php?ID=2 /page/berlin
and so on...
Is there any better (and maybe more efficient) way to do it, than to just write 2500 line of entries and their target? The problem I see is that I can not query my DB from the .htaccess file so there is not really a way to create a short rule.
Thanks,
Joel
You will have to let PHP interpret the pages for you.
The simple way is to add field more to your table, called Webname, and for the page named 'New York' the websafe name would be 'New-York'.
page.php would then have query the table for the Webname instead of the pageId.
Your .htaccess should then look more something like:
RewriteEngine On
RewriteRule ^page/?$ page.php?pageName=$1 [NC,L]
Your websafe pagename would then be available in page.php in $_GET["pageName"].
精彩评论