0

Pretty hard question.

I've got some pages with an url extension of .php?id=1, .php?id=2, .php?id=34, etc.

Now I have got on all of these pages, a <h3> containing some text.
This text I would like to have in place of the .php?id=4(i.e.) extensions.


I would like to know how I might be able to achieve this. Any help is welcome! :)



the red has to be replaced with the green, separated by a slash.

My .htaccess code so far:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ projecten-functioneel.php?args=$1 [QSA,L]
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Bokdem
  • 436
  • 3
  • 9
  • `if ($some_condition) { do_something(); }` What the condition/something are is up to you – Marc B Sep 24 '14 at 18:02
  • Could you give some visual example of what you are trying to achieve? – Alejandro Arbiza Sep 24 '14 at 18:02
  • You need this http://stackoverflow.com/questions/16388959/url-rewriting-with-php – FoxMcCloud Sep 24 '14 at 18:05
  • @AlejandroArbiza I've made an update ;) – Bokdem Sep 24 '14 at 18:09
  • I'd imagine you're looking for seo friendly urls. Something like somesite.com/Inventory/detail/22340 You can setup apache to do a [mod_rewrite][1]. Then install a skeleton framework like [panique][2] to handle the routing. [1]: http://httpd.apache.org/docs/current/mod/mod_rewrite.html [2]: https://github.com/panique/php-mvc – jon.r Sep 24 '14 at 18:11

2 Answers2

1

Please, take a look at this configuration

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^projecten-functioneel/(.*)$ projecten-functioneel.php?title=$1 [L]

The first four lines are your original settings with the only addition being the flag L at the end of the rewrite rule. This will prevent the second rewrite rule to be evaluated if the first one matches.

The last line is the one that takes care of the projecten-functioneel page request.

How it works

If the URL is

site.com/projecten-functioneel/some_title

the request will be rewritten to

site.com/projecten-functioneel.php?title=some_title  

Then in your projecten-functioneel.php file you have to ask for $_GET['title'], which in this case will be some_title.

  • When I paste the provided code on your thread, changing RewriteRule ^(.*)$ index.php?args=$1 [QSA,L] to RewriteRule ^(.*)$ projecten-functioneel.php?args=$1 [QSA,L] Nothing really changes... – Bokdem Sep 24 '14 at 18:55
  • What about, if(URL == '?id=1') then (URL -> '/afdekbanden'); I made this code up btw.. – Bokdem Sep 24 '14 at 19:03
  • Oh no, that is not where it is supposed to go. By your comment I take that you are not familiar with **.htaccess**; in which case I would suggest extreme caution: **please make back-up copies of every file you modify** so you can put everything back as it was if anything goes wrong. – Alejandro Arbiza Sep 24 '14 at 19:10
  • Thanks for the back-up tip. I've made a big mistake by not using a database so I won't either be able to work with any SQL. So, now I'm trying to work around by picking a

    title from the page and replace the ?id= with that specific title, as explained in the main question. Guess that just isn't possible anyhow.

    – Bokdem Sep 24 '14 at 19:13
  • https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ The first part they talk about, called 'What is URL rewriting', is exactly what I need. They don't explain how to achieve it though – Bokdem Sep 24 '14 at 19:16
  • I found this other question that might be more aligned to what you want to do: http://stackoverflow.com/questions/10953792/change-url-in-browser-address-bar-without-reload-existing-page I wonder if that is really what you want though. – Alejandro Arbiza Sep 24 '14 at 19:18
  • Well, in the answer to the question I linked you to in the answer, I explain how to do it. However, those rules are meant to go in the the site's .htaccess file, not in the URL nor the PHP code. You should know the basics about the .htaccess file before you try to customize it though. – Alejandro Arbiza Sep 24 '14 at 19:37
  • I know it's hard to help me out.. I've paste your provided code and changed the index.php?id=$1 to my own projecten-functioneel/php?id=1.. That just does not do the job unfortunately. I guess I'll have the url like this forever – Bokdem Sep 24 '14 at 19:55
  • Could you add in your question the .htaccess file please? – Alejandro Arbiza Sep 24 '14 at 20:01
  • I've made an update containing my .htaccess code. :) – Bokdem Sep 24 '14 at 20:04
0

There are likely (at least) two changes that will need to be made to projecten-functioneel.php.

1) It will need to change its query for articles to look them up by the keyword that is in the tag;

For example, you probably have code in there today that looks something like this:

$stmt = $db->prepare("SELECT * FROM articles WHERE id=?");
$stmt->execute(array($_GET['id']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

You will need to change this to look something like:

$stmt = $db->prepare("SELECT * FROM articles WHERE title=?");
$pathinfo = explode('/', $_SERVER['PATH_INFO']);
$stmt->execute(array($pathinfo[0]));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

2) You will need to change how links are written, so that future links are generated consistent with this behaviour.

Assuming something like this:

$stmt = $db->prepare("SELECT id, title FROM articles");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
    echo '<a href="/projecten-functioneel.php?id=', $row->id, '">', $row->title, '</a>';
}

to something like:

$stmt = $db->prepare("SELECT title FROM articles");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
    echo '<a href="/projecten-functioneel.php/', $row->title, '">', $row->title, '</a>';
}
TML
  • 12,813
  • 3
  • 38
  • 45