0

Currently, my page URLs look this this:

http://ourdomain.com/articles/?permalink=blah-blah-blah

I want to convert these to:

http://ourdomain.com/articles/blah-blah-blah

How can I accomplish this using PHP but not with .htaccess?

dda
  • 6,030
  • 2
  • 25
  • 34
Ullas Prabhakar
  • 416
  • 2
  • 10
  • 24

4 Answers4

2

How can i accomplish this using php but not with .htaccess..

You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.

If your server already happens to have AccepPathInfo On, you can try having URLs like

http://ourdomain.com/index.php/articles/blah-blah-blah

which will redirect to index.php and have articles/blah-blah-blah in the $_SERVER["PATH_INFO"] variable. This method is known as "poor man's URL rewriting" because you can't get rid of the index.php part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • As much as it would result in massive error logs, would it not be possible to use a PHP script triggered as the 404 page? Kind of a roundabout way of replicating the `!-f` And `!-d` rewrite conditions. – Luke Stevenson Feb 16 '11 at 11:30
  • @Lucaons yeah, I thought of that too but it needs .htaccess as well, as you need to specify the `ErrorDocument` directive. It would be a workaround though, albeit a horrible one, if neither mod_rewrite nor AcceptPathInfo are present – Pekka Feb 16 '11 at 11:31
  • Well, actually you can do this without mod_rewrite, using MultiViews on Apache, but that assumes either .htaccess or access to the server configuration. See my answer below :p. – wimvds Feb 17 '11 at 10:18
1

You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add Options MultiViews). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads $_SERVER["PATH_INFO"] to fetch the correct page (this of course relies on having AcceptPathInfo On).

MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.

wimvds
  • 12,790
  • 2
  • 41
  • 42
  • I understood the OP's issue more to be "I can't have a htaccess file at all". Anyway, +1, good method and might even be active already – Pekka Feb 17 '11 at 10:21
  • I just wanted to point out a valid alternative for mod_rewrite, since most people think that you always need it, which is clearly not the case. But as you point out, without any access to the server config or htaccess this approach will probably not be possible. – wimvds Feb 17 '11 at 10:24
0

I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info: Special profile page link like www.domain.com/username

Community
  • 1
  • 1
joshhendo
  • 1,964
  • 1
  • 21
  • 28
0

It is possible to do it in PHP, without modifying .htaccess

Just write following code in either index.php or default.php

<?php
if (isset($_GET['permalink'])) {
    header('Location: '.urlencode($_GET['permalink']));
}
?>

It works because when you type following URL:

http://ourdomain.com/articles/?permalink=blah-blah-blah

The filename is not specified. So, the server looks whether "index" or "default" file is present in the specified directory.

Consider file index.php is present, so server will call:

http://ourdomain.com/articles/index.php

with blah-blah-blah in GET variable permalink

The PHP code checks if permalink GET variable is present, and redirects using header() method.

EDIT: added urlencode() to do input validation

Trivikram
  • 801
  • 1
  • 9
  • 17
  • @ullasvk asked whether it's possible to do it using PHP without modifying .htaccess, so I gave very simple solution. As told by @Dan, input validation is required. – Trivikram Feb 17 '11 at 09:54