Hello PHP Friends, I need your help in a very crucial Issue. Basically I am developing my site in PHP. I want to use URL Rewriting in my site using mode_rewrite. I want my URL in this way http://www.mysite.com/development/php/basic/ this url should load the basic.php page from the php directory. here the important point is if user write something like http://www.mysite.com/development/php/basic the slash ("/") should be added automatically to the URL. and if user writes http://www.mysite.com/development/php then a slash should be added to the URL and it will load the default.php page from that directory. here you can assume that every directory contains the default page. and if user writes http://www.mysite.com/my then it should add the slash to the URL like this http://www.mysite.com/my/ and load the my.php page. Hope I have made myself clear. Please guide me in this regards, and let me know the required Rewriting rule in .htaccess Thanks in Advance....
I think the best way to do that is using a file to manage the URLs, like as we can see on Wordpress, you should create some rules on your htaccess, lets copy from the WP defaults: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> Code (markup): And on the index.php we can parse the URL and load the correct file, and when we receive an URL without the final slash you can redirect (301 recommended) to the one with "/" at the end. I hope you understand me and what I'm trying to say . Anyway... If you have any dubt, just ask.
Thanks Hernan, for ur quick response. This is indeed a better solution, but let me ask u; can't we achieve the above mentioned functionality by just tweaking the .htaccess file. Thanks again. -Deepak
It can be done in htaccess but from your description I don't think you are making things easy for yourself. It is better to stick with a predictable response across all areas of you site. Instead of http://www.mysite.com/my loading my.php it should load my/default.php
Thanks Streety, What I want exactly was; a / should added to the url, if one is not supplied with, and it should load the file named the last word of that URL string. i.e. http://www.mysite.com/my will load my.php, if that file is there and if my is a directory, it should load default page from that directory i.e. default.php or index.php, and ofcourse url should be extensionless. Can this be done with just .htaccess ? Thanks for your guidance. - Deepak
Ok, I follow now. That makes much more sense. To my knowledge that can't be done. There would need to be an element of PHP involved. I haven't seen mod rewrite used to check if a file exists. I don't think it can be done though I could be wrong. You could setup a mod rewrite rule for every URL but that would be a real pain and I really wouldn't suggest you go there.
Thanks Streety, for your Response. If you find anything helpful in this regard; please mail me: deepakonwww at gmail.com Thanks. - Deepak
PHP will probably able to help here. $urldata = explode("/",$HTTP_SERVER_VARS['PATH_INFO']); PHP: The above code can be used to break the URL after the script name (my here) into different parts. For example, site.com/my.php/home/load will be broken into 2 parts home and load. The array has to be accessed using nos. 1,2. (use while/for loop for that purpose). And the slash may not be automatically added but it will work with slash at end or without slash at end. THIS DOES NOT REQUIRE mod_rewrite. Also, to remove the .php extension, use the following code in .htaccess: <Files my file1 file2 filen> ForceType application/x-httpd-php </Files> Code (markup): If the above code does not work ask your server administrator for the defined mime type for PHP.