1

I am trying out an alternative approach to beautiful URLs with PHP:

$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));

The above snippet will let me use URLs that are something like "www.example.com/index.php/article/how-to-diy" where "/article/how-to-diy" is the URL parameter.

I'd really like to lose the "index.php", though, and I am in no way a .htaccess-wiz, so I could use some help on making a rewriterule that will change my URLs into "www.example.com/article/how-to-diy".

I've looked around on SO and the examples I found were all related to a classic parameter syntax (i.e. "index.php?page=12"), which is not the solution I am after.

Brian Emilius
  • 717
  • 1
  • 8
  • 31

1 Answers1

2

I'm accustomed to the typical MVC/Wordpress way of handling urls...less work in htaccess and all the work in the url router. I'll give an example:

.htaccess

DirectoryIndex index.php
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
</IfModule>

Then, within index.php call a routing mechanism that parses your urls and produces the appropriate page. But don't use PATH_INFO, you need to know what is being requested:

$request = explode("/", @$_SERVER['REQUEST_URI']);

EDIT:

If the script (index.php) and .htaccess are not located in the document root, the RewriteRule should reflect this:

RewriteRule . /path/to/index.php

And then the routing mechanism should simply loop through $request, in order to find the parts it needs.

Brian Emilius
  • 717
  • 1
  • 8
  • 31
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • I already tried this approach, and I get a "ERR_TOO_MANY_REDIRECTS". In my index.php I made a check to see if there are any parameters, and if not, redirect to ./home - and I already have a system set up to handle the routing. – Brian Emilius Dec 04 '16 at 19:54
  • then we need to look at your routing mechanism – WEBjuju Dec 04 '16 at 19:54
  • index.php: $request = explode("/", substr(@$_SERVER['PATH_INFO'], 1)); if (empty($request[0])) { header("Location: ./home"); } – Brian Emilius Dec 04 '16 at 19:55
  • it's probably not PATH_INFO...I think you want `$_SERVER['REQUEST_URI']`; – WEBjuju Dec 04 '16 at 19:57
  • Hmm, REQUEST_URI doesn't work in this case. From the manual: 'PATH_INFO' Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff. - Which is exactly what I want. – Brian Emilius Dec 04 '16 at 20:00
  • is your webserver Apache? – WEBjuju Dec 04 '16 at 20:05
  • Yes, I should have mentioned that in the OP. My bad :) – Brian Emilius Dec 04 '16 at 20:06
  • 1
    [this](http://stackoverflow.com/questions/2261951/what-exactly-is-path-info-in-php) says apache doesn't fill PATH_INFO. I've been using REQUEST_URI for a decade. i can't imagine it's not working if you are using apache. – WEBjuju Dec 04 '16 at 20:07
  • [wordpress](https://github.com/WordPress/WordPress/blob/master/wp-includes/load.php) uses REQUEST_URI unless on IIS servers – WEBjuju Dec 04 '16 at 20:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129750/discussion-between-webjuju-and-brian-emilius). – WEBjuju Dec 04 '16 at 20:08