I am currently trying to understand how to rewrite URL's, but I am missing several pieces to the puzzle.

I will have a directory stucture similar to the following...


I want /public/index.php to be called first on nearly every url request. The only exceptions will be for requests going to /public/images, /public/css, and /public/js. Index.php will then examine the URL and determine what to do from there. I also need this to work on my local devel box and on the production server without having to manually change little bits of code that are scattered about.

I have looked into using mod_rewrite to get the desired effect, but I would rather use php to make descisions about what the URL is suppose to mean.

Using mod_rewrite, I will have a URL full of GET data, which is not desirable, especially if I am using GET data within my pages to transfer information. And to top that off, the number of arguments in the URL could change easily, so I will have to label each argument generically, and then reinterpret what each variable is referring to when I finally get to my main PHP script. Also, coming up with regular expressions to determine whether or not I am on my local box, and then trimming the url, is something I am not sure I know how to do.

PHP will allow me to resolve absolute paths into relative ones.
'http://localhost/local/mysite/showarticle/sports/112/' will be interpreted as '/showarticle/sports/112/' very easily without mistaking 'local' and 'mysite' as arguments that need to be processed.
And, I wont need to use any GET data to represent the action I want carried through, php will be able to interpret the URL internally without needing to modify the URL into 'http://localhost/local/mysite/public/index.php?' with a bunch of GET data at the end.

Ideally, I would like to set up a handler in apache (through .htaccess only), that calls index.php for any url that doesnt contain a file at the end ('http://localhost/images/pic.jpg' would be ignored). But as far as I know, handlers are only able to call cgi's (this could be bad information though), and I do not have php installed as a cgi.
I would like the handler to just straight up ignore the URL, call index.php, and pass the URL as an argument.

Is there any way to use the PHP method without using CGI's? If not, how hard is it to set up CGI's (never used em)?

If the PHP handler wont work, where do I start in doing a URL rewrite. I am starting to understand regular expressions, and all of the rewrite rules and rewrite conditions, but Im not exactly sure where to begin in parsing the URL and then making sure my index.php ends up with a valid url that it can interpret.

I know it was long, but thanks for pushing through it.

Member Avatar for fatihpiristine

yeah this is so long storey..you should keep your questions shorter :P otherwise ppl runaway

i did this yesterday for myself .htaccess

RewriteEngine On

RewriteRule ^\.htaccess$ - [F] 
RewriteRule ^\.jpg$ - [F]
RewriteRule ^\.html$ - [F]
RewriteRule ^\.css$ - [F]

RewriteRule ^([0-9]+),([0-9]+)$ /index.php?Language=$1&Index=$2 [L]
RewriteRule ^([0-9]+),([0-9]+),([0-9]+)$ /index.php?Language=$1&Index=$2&Category=$3 [L]
RewriteRule ^([0-9]+),([0-9]+),([0-9]+),([0-9]+)$ /index.php?Language=$1&Index=$2&Category=$3&State=$4 [L]
RewriteRule ^([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)$ /index.php?Language=$1&Index=$2&Category=$3&State=$4&Position=$5 [L]
RewriteRule ^([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)$ /index.php?Language=$1&Index=$2&Category=$3&State=$4&Position=$5&SubCategory=$6 [L]

if you need more about this.. search for apache rewriteRule

Thankyou much. I think I understand how to rewrite URL's after looking at your example.

I would still like to avoid using mod_rewrite if at all possible though. I would rather not have to rewrite the URL, but instead use php to interpret the URL as is. The idea of using a handler to do my logic seems more appealing, but I am still not sure how to do this.

Thanks again

Member Avatar for fatihpiristine

i worked on php but i was not successful and i pick this way.

I have been googling for a long time and ran across this little bit...

<Files index>
	SetHandler application/x-httpd-php

Will this bit of code work if PHP is not installed as a CGI? And, why did this person use the SetHandler directive instead of ForceType.

Member Avatar for fatihpiristine

I have been googling for a long time and ran across this little bit...

<Files index>
	SetHandler application/x-httpd-php

Will this bit of code work if PHP is not installed as a CGI? And, why did this person use the SetHandler directive instead of ForceType.

could you post the link here..?? i wanna check it.

This article goes through the whole process of configing the .htaccess all the way to actually interpretting the results with PHP. This post was put up in 2001, so Im thinking the PHP script is PHP4, but the apache .htaccess looks like it should work.

This article is very similar but does not dig into the PHP details as much...

And finally, here is the original article that I quoted. It seems to be for a windows/apache setup, although that shouldnt matter. He does not every say whether PHP is installed as a CGI binary or mod_php, so I am not sure about using "SetHandler" over "ForceType"..

All of these articles take a similar approach to setting up clean URL's, and it does not involve the use of mod_rewrite at all, albeit these are simple applications, so mod_rewrite may still be necessary for better control.

If anybody sees a problem with any of these examples, please let me know. I havnt been able to test it yet, but it seems the solution I was looking for is this.

<Files index>
	ForceType application/x-httpd-php

This would work for a link like this... 
...where you have a PHP script named 'index' (no .php at the end) in your web root along with the above code put into an .htaccess

thanks for your posts fatihpiristine, hit me back with some feedback on this latest post

Member Avatar for fatihpiristine

thanks.. i ll work on this... i have so much free time :) if i do something really useful, i ll post you for sure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.