I want to handle all URL requests manually like express js in node.js does. And I don't want to use any library because I want to learn and extend my knowledge.
if( url === "/profile") //... load profile.php file
This is just a common use case. But if we manage to get all requests in a specific file, then They can be filtered with method and that's will be great for API endpoint handling.
If I use .htaccess in apache to redirect all request in a base file and then handle it, It will cause some problems, like
htaccess rewrite is not enabled in all sever by default
and that could be a problem in nginx server
and many more ...
I want to do it without any server configuration.
My questions are
Is it possible with only php? I mean without server configuration.
How php itself handle a request?
Please help with this problem
Related
I have a site running WordPress on Apache server and I am attempting to provide both HTTP and HTTPS connections via the same site. I want to allow connections over HTTP without forcing a redirect to HTTPS, unless the client is connecting initially via HTTPS then I want all subsequent HTTP requests to be forwarded to HTTPS to avoid issues with CORS and unsecured content warnings.
I am having some trouble turning up results on how to effectively do this with mod_rewrite alone. Most solutions I find try to force the connections to redirect to HTTPS regardless and will not allow an HTTP connection or vice versa. I have tried a few mod rewrite conditions including making use of the referer string but none seem to work thus far. I must be missing something because I feel that this is indeed possible but I and my search engines alone are stumped.
Maybe I'm just doing something wrong or is this kind of functionality beyond Mod_Rewrite?
I was thinking to use a PHP script but was worried it wouldn't work for some static files since WordPress doesn't handle those requests.
Update:
I have made a php script to detect the version. It sets a cookie which expires in 20 seconds from being set, this is read by Mod_Rewrite and if set it redirects the URLs to HTTPS. This works for most of the subsequent requests of an initial HTTPS request. A few URLs seem to be unaffected by it, not sure exactly why as the cookie hasn't expired by the time of these file requests and the particular rules are before the static file bypass rules in the htaccess file. At any rate that was easy enough to fix by setting the file urls to protocol-less versions.
Some third party sites need domains rewritten though, as they serve https from other domains. On that note I don't think this is actually possible without buffering the whole page and actually re-writing the URLs.
It is possible to detect the initial connection but this must be done using Server Side code, like a PHP script. Then using the detection can be done at Mod_Rewrite level.
Add in the WordPress constraint and things get complicated.
WordPress isn't built to facilitate one install with both protocols allowing access to content. So to accomplish this would require a custom plugin using the detection mentioned earlier, and instead of using Mod_Rewrite to direct requests on the server, we have to buffer WordPress output and logically replace/rewrite URLs in the page before they go to the user if and only if the initial connection for the page is in SSL.
There is only one plugin I have found which does something similar to this, however it doesn't do dynamic detection only gives admin/editors a checkbox option to make a page SSL secured. The plugin is called WordPress HTTPS
Dynamic detection and redirection isn't something SSL was meant for anyways, it's either on or off, and most pages need it that way.
I was originally trying to provide both so I could use a self-signed certificate without worrying that users would get the "warning unsecured connection" messages from their browsers by forcing them to use only SSL connections.
So I'll be purchasing a cert or making a custom plugin.
tkausl is right, you don't really need to do mod_rewrite. You should be able to format links without the protocol and it will automagically select for you.
You can see that google does this with their hosted libraries:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
*Note the lack of http: or https: this will follow the protocol requested by the user.
So I am confused on how to properly implement the Slim Framework into my use case.
I have a web app html/css/javascript that can not have PHP in it.
All I want to do is use Slim to do some simple GET requests via ajax.
How do I start the App to handle requests if it is never touched int he index.html file?
I'm guessing this has something to do with the .htacess file but not finding anything useful in my initial search.
Got some help on this from the Slim Discussion board.
Here is the answer and example.
Yes, the .htaccess file ensures that any request is handled by a single file (usually index.php but you could name it anything you want).
Your Ajax calls would presumably go to different URLs. Those requests would all be handled by the Slim app.
Example Here
hi i am not sure if this is restful related, i think it is , but please do correct me if i am wrong.
so basically i want my server, which is written in PHP, to respond to different api requests, so for example
http://www.myweb.com/api/content/video/get?id=1 which will return a json object that has information regarding of a video of id=1
however, i can also have the following api to be called
http://www.myweb.com/api/content/music/get?name=biever
i want to create a centralized dispatcher, a php file that sits in the api directory in the server, so whenever a request is made where api appears in the RESTful link (which is in both example above), it shoudld 'intercept' these requests, and examine rest of the path to call the function accordingly. so for example
in controller.php in api directory
it will see, oh you are calling content/video, i will do some pre processing (say, adding video name or something) then direct the method call to content/video/get
how can i, in php, make sure that all the requests will go through my controller file first?
By default URLs are mapped to PHP files in your file system. You can't control this in PHP. By the time your PHP is run, you're too late. So you'll need to look into rewriting the URL at the webserver-level. If you're using Apache look into mod_rewrite.
I was wondering how Wordpress and other random forums and sites, like facebook, create like phantom directories? Eg, a blog might have http://www.joesblog.com/2010/11/12/this-is-my-post.php
Does that file and directory resource actually exist? Also, how does Facebook, for example, have like http://www.facebook.com/-usernamehere- ? Is that a physical directory, or is it simply a scripting trick? How can I do this with PHP?
That kind of functionality is normally achieved by instructing the web server to "link" certain URL patterns to a specific controller.
See .htaccess, for example.
EDIT: This article on Rewrite engine might also help.
So no, no directories actually exist. The web server receives a request for a specific URI and redirects that request to a delegated controller (that can be a PHP script, for example) that in turn returns a result based on the URI and action requested by the user.
PHP can certainly handle this, but it's the web server that needs to be instructed on how to handle those types of request.
If you're using apache you might want to take a look at some mod_rewrite tutorials.
On of my client asked me to create an Web App in PHP, I ended up using Symfony. At delivery, he told me that he has distributed a software with an embedded Web view pointing to a hardcoded url :
www.domain.com/dir/tools.php
Now he wants the Web app to appear in it's Web View, but the software isused by about 400 customers, we can't expect the hard coded URL to be changed.
How do you think I can do that in clean way :
Create www.domain.com/dir/tools.php and use a redirection ? Which one and how ?
Use URL rewriting ? Any snippets appreciated, I have no Idea how to do that.
Apache mod_rewrite:
RewriteEngine on
RewriteRule ^dir/tools\.php$ new_page.php [R=301]
EDIT: As noted, this goes in your .htaccess file. The mod_rewrite documentation I linked has more information. Fixed .
In your Apache configuration for the host or in a .htaccess file, you can do a redirect:
Redirect 301 /dir/tools.php http://www.example.com/whatever
Use URL rewriting.
URL rewriting is implemented with a couple lines of configuration in your web server software. It is not implemented in your own web application, so you would not be using PHP to implement it. You will be telling your web server that any request that comes in asking for "/dir/tools.php" will be modified, before it ever hits your PHP web application, to ask for whatever URL you want it to ask for. For example, you can specify that any request asking for "/dir/tools.php" be rewritten to ask for "/my-custom-dir/my-tools/" instead.
Each web server implements URL rewriting differently. Often, the web server will not support that feature with a minimal out-of-the-box feature set, but through an addon that you can install. You should consult the documentation for your web server to find out how to configure it to perform URL rewriting.