PHP Dynamic Folders - php

Using PHP, how can you make a dynamic folder system, like http://website.com/user/UsernameHere/ show something like http://website.com/user.php?name=UsernameHere
Does this require server configuration, or is it possible with PHP?

Typically this will rely on some amount of URL-rewriting support from the web server, for instance, some mod_rewrite rule under apache.
Whether or not it's a bunch of complicated rewrite rules, or just a simple one that rewrites everything to /index.php (and let's index.php parse and interpret the URI), doesn't matter.
But you'll need some amount of web server configuration to get it going.

There's a few techniques for doing this. It's nearly possible to clean up your URLs using just PHP, but ultimately if you want to get rid of the ".php" extension you'll need to do something with your server. If it's Apache you can clean up using the .htaccess file.

Related

Simplifying URL without using .htaccess

How to modify URL such www.mysite.com/dir.php?ID=123
to www.mysite.com/dir/123 without using or involving .htaccess
cause I can't access .htaccess on our server.
If this is your own application and not the standard framework/CMS then you can implement your own rewrite engine in PHP so the URLs would look like www.mysite.com/index.php/dir/123.
That's slightly worse than the clean URLs but that's the best you can achieve.
When you run the url like that the $_SERVER['PATH_INFO'] PHP variable would store "/dir/123" which is then your job to parse and transform to the params you need and then invoke or include the right script.
IIRC Kohana is built like that. So you can look their code for an inspiration.
The only other option is using a rewrite engine on the web server software, e. g. mod_rewrite for Apache.
But as long as you don't even control files on your server, you probably don't have access to Apache configuration.
Your web app/CMS should have support for nice URLs too.
Short story is:
You can't
At least you should be able to use .htaccess to normally do this. If you can't even use that, other options will most probably also be limited out for you.
But there is one work-around that you MAYBE (I'm absolutely not sure) can use...
Are you allowed to set your own 404 error pages?
If so, try setting a 404page.php that acts as your entry point, sends out a http_response_code(200); and does what it further should do.
Off course you should remove any index.php from your public_html and not use any of the URLS that should be handled by the handler, so they will lead you to the 404page.php.
(Let me know if this worked :). )

Is it bad to force the use of .htaccess

I noticed that most of the frameworks (CodeIgniter for example), do provide a default .htaccess file but don't force its use.
Why don't they force its use?
Does .htaccess work on all servers?
What are the alternatives?
.htaccess files only work on apache servers. When using other servers it highly depends on what you want to do - but usually you need to edit the server config to rewrite URLs, block directories, etc.
The fact that frameworks need .htaccess files is actually an annoying problem from the PHP world since 99% of all applications are stored inside the document root, thus giving users HTTP access to all their files unless they are somehow restricted (e.g. via .htaccess). On the other hand, if you have a WSGI-based python application, you usually store it outside the document root and "mount" it to a certain folder in the document root - this way not a single file can be accessed directly via HTTP.
1) I have to agree with Juhana. The question is: why should they force it? There is no need to restrict a framework with such a thing.
2) I heard that they are not working on IIS Server, where you have to translate it in special config files.
3) It depends on what you're doing. But because of the fact you do not really need .htaccess files, just let them be is a possible alternative :)
Because it does not work on all servers (esp. hosting providers can restrict its use) and there is really no good reason to enforce it.
See #1. Also, they tend to not work in the same way if you're not running Apache.
Very wide question. If you're running Apache it's pretty much the only way to configure the server while not being a privileged user. If you're not running Apache, it's dependent on the specific web server.
Some useful links;
Apache htaccess to nginx rewriterule converter
How to translate htaccess to IIS web.config
How to work around lighttpd's lack of directory specific config

Apaches mod_rewrite VS. PHP routing?

for some days now I have been trying to make a simple mod_rewrite rule to create friendly URLs, my web host have mod_rewrite enabled but I just can't get it to work.
All the next questions where posted by me:
.htacces to create friendly URLs
Friendly URLs with .htaccess
.htacces NOT working…
None of the answers worked, so I'm thinking now using simple php routing instead and I wanted to know if there is a big performance or SEO difference between the two. And if there is, maybe you know how to fix the mod_rewrite problems posted in my questions.
Thanks.
If you're using PHP routing for PHP files only, it would be no problem performance-wise: The interpreter will get started anyway, a new process started, memory allocated etc.
But if you are planning to route requests for static resources like images and style sheets as well, however, don't use PHP routing under any circumstance. It's way too resource-intensive and not what PHP was built for.
I'd say mod_rewrite is the better, leaner solution and it's worth trying to figure it out.
I prefer routing that kicks in when the requested file doesn't exist, like this in Lighttpd:
server.error-handler-404 = "/index.php"
Provided you find out how to do this in Apache, your script would be more cross webserver compatible, since Apache's mod_rewrite conditions in .htaccess won't work on Lighttpd.

How to hide the url in php

Is it possible to hide the the url in the address bar of the web browser so that it won't necessarily match the location of the files.
For example, this url:
http://localhost/exp/regstuds.php
You will always know by looking where to find the files in the computer.
Is it possible to distort or disarrange or hide the url in such a way that the location of the files will not be revealed
Yes, if you're using Apache look into using mod_rewrite. There are similar rewrite modules for pretty much all other web servers too.
I hope your sole motivation for doing this is not "security through obscurity". Because if it is, you should probably stop and spend more time on something more effective.
If you are hosting your php on an Apache server, you probably have the ability to use the mod_rewrite utility. You can do this be adding rules to your .htaccess file...
RewriteEngine on
RewriteRule ^RegStuds/ regstuds.php
This would cause http://localhost/RegStuds/ to actually render regstuds.php, but without ever displaying it in the address bar.
If you are on IIS, you can perform the same function using an ISAPI Rewrite Filter.
If you don't have mod_rewrite or an ISAPI Rewrite Filter, you can get a similar result using a folder structure, so you would have a physical path of RegStuds/index.php - and you would never need to link to "index.php" as it is the default file. This is the least recommended way of doing it.
No its not.
Each bit of functionality must have a unique identifier (URI) so that the request is routed to the right bit of code. The mapping can be non-linear using all sorts of tricks - mod_rewrite, front controller, content negotiation...but this is just obscuring what's really going on.
You can fudge what appears in the address bar on the browser by using a front-controller architecture and using forms / POSTs for every request but this is going to get very messy, very quickly.
Perhaps if you were to explain why you wanted to do this we might be able to come up with a better solution.
C.

Running other file types as PHP

Is there any problem with running HTML as PHP via .htaccess? such as security or best practices etc. was doing this to make URLs cleaner.
## run the following file types as php
Addhandler application/x-httpd-php .html .htm .rss .xml
Well ideally id like to have my URLs like
localhost/blog/posts/view.php?id=64
to be
localhost/projects/bittyPHP/bittyphp/posts/view/id-64
But having trouble accomplishing that without routing everything to one file and having PHP run determine the paths. I guess this is my real question
I would use mod rewrite.
Probably you do not need to run all html files as PHP, and if you have short_tags enabled "<?" in XML will give you trouble.
Keep in mind that you will run each and every of those files through the PHP handler then. If there is no PHP inside the files, the parser will still inspect them to see if there is any PHP in it. This adds some overhead, but it is likely neglectable in most setups.
Main issue I would say is performance. If you have a significant number of plain HTML files then you're creating unnecessary overhead by always running them through the PHP interpretter.
Best practice is not to do this, but use "friendly" URLS like mysite.com/item/123 and use mod_rewrite to convert them to mysite.com/displayitem.php?id=123 internally
Like many people have already stated, mod_rewrite is the best solution for accomplishing friendly URLs.
Sitepoint has a decent guide to getting started with mod_rewrite.

Categories