2

Possible Duplicate:
.htacces to create friendly URLs. Help needed

I would like to have create a website, which loads up content from a MySQL database via PHP, while still being optimized for search engines.

I have considered a few approaches and was looking for some advice on which way would the best.

Users on my website would fill out a form and submit it, which gets saved in the SQL database. I would like to have it so that each new page could be accessed like this:

  • domain.com/plugins/whatever
  • domain.com/plugins/anotherpagename

Some approaches: 1. A script generates a static HTML page in the plugins folder when the form is submitted. When the page is edited (there will be a edit button), the HTML page gets overwritten with the updated information.

  1. domain.com/plugins/whatever and domain.com/plugins/anotherpagename would all redirect to a PHP script.

However, I do not know how to create a .htaccess which would allow the PHP script to get the page name so that it can search the SQL database and bring up the correct information.

I am also unsure about how search engines would treat these pages?

  1. Maybe someone else can come up with an even better approach for this?

Thank you in advance for your help.

Community
  • 1
  • 1
Mastergalen
  • 4,289
  • 3
  • 31
  • 35
  • As long as there's a unique URL per page it does not matter at all what you're doing on the backend. The URL can be `example.com/plugin?page=whatever`, which would be really simple to set up and just as good for SEO as anything else. But you'll want to go with the above linked URL rewrite solution... – deceze Jul 25 '12 at 10:47

3 Answers3

2

You have a few options for how to do this. The simplest way that matches your requirements would be to add the following to your .htaccess file:

RewriteEngine on
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L]

The above pass all requests to /plugins/anything to yourscript.php, from which you can access the plugin name using $_GET['plugin'].

If "plugins" is simply an example, and you want to be able to use /anything/anything - then you may wish to look at passing all requests to your PHP script, and then parsing the requested URL.

To do that, you'd use something like this in your .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /yourscript.php [L]

To break that down, the above tells Apache to rewrite all requests where the file does not exist (i.e. images/css/js, etc.) to yourscript.php.

From there, you can access the whole URL via $_SERVER['REQUEST_URI']. Which you can break down with string functions, regular expressions, and so on.

Dan
  • 550
  • 3
  • 7
0

why not make an actual directory ? Eg.

domain.com/plugins/whatever domain.com/plugins/anotherpagename

whatever and anotherpagename would both be directories. Then write the html file as index.html inside the directory. No htaccess needed and you could still access using the same url as above.

chip
  • 599
  • 1
  • 9
  • 20
0

I don't know if you are using any framework. I have solution using codeigniter. The same can be simulated using other frameworks / core PHP.

codeigniter specific solution:

  1. In config/routes.php define

    $route['plugins/(:any)'] = "plugins/getPageFromDb/$1"; After this, in browser the url will be same 'domain.com/plugins/whatever' but actually control will hit 'getPageFromDb' function of the 'plugins' controller with 'whatever' as the parameter.

  2. In 'getPageFromDb' function you should write your code to get the page from db based on the parameter 'whatever' and echo the output directly or through a view template (depending on your logic / scenario).

And for performance you may keep a static version of the html page and write a relative check in the 'getPageFromDb' function to decide whether to get the page from static or from db.

Considering the url format and the well formed HTML rendering, it should be Search Engine friendly page.

G S Bajaj
  • 84
  • 1
  • 7