How to make form shows only value in the URL? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to build an online Malayalam to English and English to Malayalam dictionary.
There are two online dictionaries available, however it is not perfect so I have plan to build a good dictionary.
When I check some of the website both of them are used get method at the same time URL details are totally different method. Let me show how it working:
At the same time my website showing like this:
Is there any option to my URL like other two websites? I think both websites are using PHP with jQuery.

try this
RewriteEngine On
RewriteBase /
# make pretty urls work
RewriteRule ^dictionary/([^/]*)$ index.php?ml=$1 [L]
# redirect none-pretty urls
RewriteCond %{QUERY_STRING} ml=(.*)
RewriteRule ^$ /dictionary/%1 [L,R=302]
You'll also need some Javascript to catch the form's onsubmit, and change the url to not have the get parameters. You could do without, but that would result in an extra 302 request, and slow the pageload down a bit.
(PS make sure you php script return a 404 page if it can't find the word in the database.)

You need to use the mod_rewrite from apache2, it's a way for mask your parameters in a user friendly url.
You can read about thins in this tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
full docs: http://httpd.apache.org/docs/2.2/rewrite/

Related

handle all forms for one resource on the same page (php) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
currently I am doing CRUD task for a resource "events" and for each task I have one page that the form for that task submits to (i.e events/create.php, events/read.php, events/update.php, events/delete.php). I would like to instead handle all of this one one page (sort of like with Ruby on Rails when there is an EventsController with index, show, put, and delete methods). I would not like to use a framework. How can I do this in raw php?
That is achieved by two things.
Firstly, direct all traffic to this page by manipulating your server configuration. In the case of Apache, that is most often done by placing a .htaccess file in the webroot of your project with something along the lines of:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [L]
Which will direct all traffic to an index.php.
Secondly, your PHP script must be capable of translating every request into something meaningful by capturing at minimum the request URI, method and body.
Because you are not interested in using a framework, you'll have your work cut out for you. As a start, you should look at $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'] and capturing the raw request body with something like:
$body = file_get_contents('php://input');
Once you have this data you have to decide how you want to trigger the appropriate functions in your script based on the above, which involves parsing the URL, checking the request content-type header to decide how to parse the input and so on.
If you decide this is all too much work given there are dozens of premade solutions, you should look at FastRoute which is a very popular library that covers everything I've written here.

How can I make a folder expire and not serve up its contents after period of time? Using Apache [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a hosted Apache based website. Currently, we create a new project folder on the site, and dump the flash based course up for review. Is there a way to make the content folder expire after a certain amount of time so that the content is no longer view-able?
Please let me know if you need any further clarification.
Yes, you can, using mod_rewrite module.
For example, to deny all requests since November 2014 you could use these rules:
RewriteCond %{TIME_YEAR} =2014
RewriteCond %{TIME_MON} >10
RewriteRule .* /deny.html
RewriteCond %{TIME_YEAR} >2014
RewriteRule .* /deny.html
To see all the available environment variables check the link below.
PS: you obviously need to provide some nice /deny.html page so that it was obvious for visitor what happened.
Or if you just need a standard 404/403 use the following rule:
RewriteRule .* - [R=404]
References:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Open image in a page if users try to access directly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
One thing I've noticed on Facebook is when you access an image/video directly, you can view it, even if the uploader specifies that you're not able to see it.
So I was thinking. Is it possible to redirect any image that is accessed directly (eg. www.example.com/img.jpg) to a page that has that image on? So say you were to access www.example.com/img.jpg, it'd redirect to a specific page for that image (perhaps the page is named after the image or something, so like it'd redirect to www.example.com/img.php).
I imagine it could possibly be done with .htacces, but I'm not advanced with that.
Anyone ever achieved this before and know if it's possible?
Something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*)\.(jpe?g|gif|png)$ /$1.php [L,R]
redirects any request for an image that isn't referred to by your site (www.example.com) to the image's name but with a .php extension instead.
Of course, this only works if you have a php file named after each of your images. If you want just a single script to process all your images (might be easier if you have a lot of images), you can do something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*\.(jpe?g|gif|png))$ /image_page.php?image=$1 [L,R]
And you'll have a script image_page.php take the $_GET[] parameter "image". And you can output a page that links to the image.
If you'd rather not redirect the browser, just remove the ,R flag inside the square brackets.

URL Shortner... Mod Rewrite [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to build a simple URL shortner and I have come across a few complications:
A) in .htaccess how would I rewrite mydomain.com to make it so users can enter mydomain.com/4854 while the page is actually mydomain.com/url.php?key=4854.
B) If I do have that Rewrite rule how can I be sure to bypass it for index.php and add.php
Thanks so much in advanced.
In your .htaccess you can do:
RewriteEngine On
RewriteRule ^([0-9]+)(/)?$ /url.php?key=$1 [QSA,L]
This rewrites requests that contain only numbers to url.php.
Then in url.php, if key is not found, redirect to error page.
RewriteEngine on
RewriteRule ^([0-9]+)(/)?$ url.php?key=$1
And if the key is invalid, redirect in the php file
Let me save you the grief...I was doing the same thing, and found it written for me at yourls.org. It covers pretty much everything you need, as well as giving you option user pages to add their own, AND gives you an API interface for others to use the shortened URLS. I had it up and running in minutes, and spent a couple of hours modifying the samples given to be ready for production.
Even if you don't use it, the mod_rewrite solution is there for you.

PHP: Two basic questions about URL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Question 1
www.example.com/?do=blah
How do you get it to work without index.php? Any examples?
Question 2
www.example.com/news.php?blah
This link doesn't show the same result as news.php, but shouldn't we be using news.php&blah=value for example? What does the string after ? stand for? Shortened version of GET variables or an entirely different thing?
Question 1
As long as index.php is your default and you have it present in your root folder then you only need to have www.domain.com/?querystring=value
Question 2
? is the beginning of a querystring parameter. It should be used for the first one.
& is for every querystring parameter after that.
index.php?querystring1=value&querystring2=value&querystring3=value and so on.
You can configure a URL rewrite using .htaccess for both of 'em.
RewriteEngine On
#for question 1
RewriteRule ^(.*)$ index.php/$1 [L]
#another example for question 2
RewriteRule ^news.php\?(.*)$ news.php?blah=$1 [L]
The first example will capture everything and redirect the user to the index.php file. So if the user tries to access domain.tld/abc they'll actually be accessing domain.tld/index.php/abc
As for the second example, it will grab the everything that is part of the query string (a better Regex might be needed). Basically, it'll turn news.php?value to news.php?blah=value
I don't entirely understand your second question, but the answer to your first question is that you need to specify a different default directory index on your webserver. Assuming you are running Apache, find this line in the httpd.conf file:
DirectoryIndex index.html index.php ...etc
and change it to:
DirectoryIndex your_preferred_default_page.php
For your second question, the question mark is not shorthand, it marks the beginning of a query string... it's used as a cue to treat futher strings as GET keys and values.

Categories