posting data to a web page,need an alternative - php

To request some data from a web server, we can use the GET method,like
www.example.com/?id=xyz
but I want to request the data like
www.example.com/xyz
How can it be achieved in PHP?

Create a file in your root directory and call it .htaccess. Put this in it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [R=301,L]
If someone goes to www.example.com/xyz and xyz is not a directory or a file it will load /index.php?xyz instead. It will be completely transparent to your users.

You could use mod-rewrite, some more info is here
http://www.trap17.com/index.php/php-mod-rewrite-tutorial_t10219.html

I'm not sure "posting" the data is the right terminology, but you can use Apache mod_rewrite to make URLs like '/xyz' direct to your PHP application. For example, place a .htaccess file in your web root with the following,
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now the URL specified is available in $_GET['url].

I don't think you can achieve what you want with the GET method, as PHP will always append form data in a query string to the end of the URL specified in your form's action attribute.
Your best bet is to post the data to a handler (i.e. www.example.com/search) and then use that page to redirect to the correct page.
So if you entered a query for hello+world, that variable would be passed to your /search page and processed by the PHP script to re-direct to /hello+world.
Of course, you're going to need the correct .htaccess rules in place to handle searches like this, as well as sanitizing data.

Related

How Do I Make A Common PHP File To A Directory With .htaccess?

I'm looking to handle the URL's except homepage with a common PHP file. This is just like a PHP $_GET request except the difference that there would be no parameter. It'll be just like a file.
Ex- http://localhost/ - This should be managed by index.php file as usual.
http://localhost/ANYTHINGHERE - This should be thrown to a custom PHP file which would then decide what to do.
Actually, I'm working on a project where I need to hide the URL information from the users. So, the file that would manage the ANYTHINGHERE URL would actually access a directory localhost/i/.
Thanks and waiting for best response!
To achieve this you need two parts:
First: .htaccess which redirects all accesses to your domain passed to a php script (index.php here):
RewriteEngine On
RewriteRule (.*) index.php/$1
Second: In index.php you get the user-entered URI as $_SERVER['PATH_INFO'] (starting with /)
This, however, makes all requests to go through the index.php script (depending on the location of index.php you could also get an endless recursion, so read on ;) ). Normally one doesn't want that (e.g., images should be served directly by the web server). Thus, one normally uses (i.e., existing directories, files and links are served by the web server directly):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [NC,L]
RewriteRule (.*) index.php/$1
If this should take place in a subdirectory you need to add RewriteBase /subdirectory directly after RewriteEngine On.
If you don't want to use $_SERVER['PATH_INFO']you can also use RewriteRule (.*) index.php?url=$1 [QSA], then you get the user entered URI as $_GET['url'].
This requires mod_rewrite to be loaded on the server.
See http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html.

something messy with .htaccess while using php include

I am currently working on a web application where i want to give user access to their profile by typing www.mysitename.com/username this is working fine with .htaccess but i also want the user to access other pages with clean urls by redirecting
http://www.mysitename.com/song-type-one.php?song=xyz to
http://www.mysitename.com/song-type-one.php/song/xyz
i used the rewrite rule like this
RewriteRule ^(.*)$ ./song-type-one.php?song=$1
RewriteRule ^lok-songs/(.*)$ .song-type-one.php?song=$1
if i type the clean url now in the browser only my main content is loaded the css, javascript and my php include are not loaded. there might be something wrong with my .htaccess whats the best way to achieve all rules i want with .htaccess ?
Try adding this before it:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
If the file exists on disk it won't do your rewrite rule allowing the JS and images to be loaded normally.

htaccess redirection loose post data!! how to keep post data while redirect using htaccess?

I have a .htaccess file that rewrite .php to .htm:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s
RewriteRule .* %1.htm [L,R=301]
RewriteRule ^(.*)\.htm$ $1.php
The redirect works fine, But the redirection looses the $_POST data. How to keep $_POST data while redirecting?
Browsers convert a POST request into a GET request on redirect. The RFC states that they should instead prompt the user if the method needs to change, but none of them follow that and instead force the method change unconditionally.
If you want to simply rewrite the URL from .php files to .htm files, all you'll need to do is something like this -
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s
RewriteRule ^(.*)\.php$ $1.htm
The 3 lines of the .htaccess detail the flowing behavior -
Turn on the rewrite engine
The request matches the given regular expression.
Rewrite the entire URL to use a .htm file.

How to redirect URLs so that they are all handled by the same PHP page

I know that there are many questions about this subject, but the questions, and even more the answers are kind of confusing me.
What I want to do:
I want to have an internet page, wich, depending on the URL, shows different content. However, in the backend, all pages are handled by one central PHP page.
In other words:
www.example.com/
www.example.com/AboutUs
www.example.com/Contact
should all be handled by a single .php script, but in such a way that in the browser of the users the URLS are kept intact.
Now, is this done with .htaccess rewriting or not? And how?
.htaccess using Rewrite would be the best approach for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
In your index.php you can use the value of $_GET['uri'] or $_SERVER['REQUEST_URI'] to determine which functionality is being requested.
If you only want your controller script to handle requests for files and directories that don't already exist, you can do:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Yes, you can achieve this by adding mod_rewrite rules to your .htaccess file. Here is an article with more detailed information: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/.
It may not help your confusion, but it will at least teach you the proper syntax. Basically, mod_rewrite takes the "clean" URL given in the browser, decodes it using a regular expression, then discretely passes the matches from the regular expression as GET variables.
In other words: mod_rewrite takes "example.com/AboutUs", reads the URL, and serves up whatever would be on the page "example.com/index.php?page=AboutUs" without showing users the actual GET-variable-ridden URL.

Create a Catch-All Handler in PHP?

I want to have a PHP file catch and manage what's going to happen when users visit:
http://profiles.mywebsite.com/sometext
sometext is varying.
E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.
My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.
Thanks to those who'd answer!
Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,NC,QSA]
</IfModule>
It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.
Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.
You should use a rewrite rule..
In apache (.htaccess), something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
Then in your index.php you can read $_GET['url'] in your php code.
You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.
An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.
If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profiledomain.com/usernam/profile/editdomain.com/usernam/inboxdomain.com/usernam/inbox/read/messageid Or use .htaccess wisely

Categories