I am looking to do something along the lines of what is shown here:
http://strawpoll.me
When you create a poll on there it gives you a unique url to which you and anyone else can access it by visiting that url.
Now obviously they can't be cluttering up their server with thousands of individual files containing the polls and I'm assuming they have one basic template to which they load the poll information from a database.
I want to make a form and when the user submits it they get their own unique link like how it is on the strawpoll website.
As is it is a simple dynamic page creation requiring only few files...
The poll creation page which is static
The processing.php file which collects the data and inserts into the db
The dynamic poll rendering page polls.php which renders the poll based on an auto-incremented id assigned to the poll.
The polls-results.php which returns the results.
http://thepollsite.com/12345
.htaccess is used to identify which poll to render as well as which results to render.
# Turns on Mod-Rewrite Engine
# ----------------------------------------------------------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Poll Pages
# ----------------------------------------------------------------
RewriteRule ^([^/]+)/?$ polls.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/?$ polls-results.php?id=$1 [QSA,L]
On both polls.php and polls-results.php you get the id with: $poll_id = $_REQUEST['id']; and process from there.
The embed code is rendered in a simple dialog pop which provides the iframe code as shown.
Though there are frills you can add... its is really a simple process.
On the .htaccess side, you'll want something like:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
With that in place, all requests for files/directories that don't already exist on the server will run index.php instead of showing a 404 page. Then, to get the requested path, use $_SERVER['PATH_INFO'] in your index.php.
This is the general method of using "clean" URLs with PHP. It's probably already been said in another SO answer, but it's hard to find so I wrote it again.
Related:
Why use a single index.php page for entire site? and How to do URL re-writing in PHP?
Related
I am deciding to create separate profile links for each user who registers on the website. I am using the .htaccess file RewriteRule to achieve this. The url I want to achieve should look something like www.domain.com/username. (I don't want www.domain.com/users/username)
Now the problem is, if I add a rule like RewriteRule ^(.*)$ /users.php?username=$1
it will matchup all URL addresses for www.domain.com, which may direct to any path. Even if I would like to visit www.domain.com/about page, it will be redirected to
www.domain.com/users.php?username=about, which I don't want. (Even if the requests was www.domain.com/products/abc)
I could apply filters in my users.php to filter such usernames, or if a username is not found in database, redirect to the page, but this means I have to change filters every time I decide to add a new page in my directory (in this case, any). Also, all traffic will be directed through 1 page, which may cause a performance problem.
Please suggest a way to achieve this, as There are websites that work like this. A possible RewriteRule or PHP code to achieve this.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /users.php?username=$1 [L,QSA]
I always use just simple rewrite as below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
All traffic is redirected to index.php and using php I can run specific controllers depending on url. You should also think about. Almost all frameworks use such rule.
Then in your PHP file you should examine
$_SERVER['REQUEST_URI']
variable to route request to specific controllers
I am creating my own mvc framework to use in little projects and by default, I am rewriting the url so that every single request goes to index.php. Index.php is only 4-5 lines, which calls the application class and then, the application class calls the corresponding controller and so on.
Basically, this is my htaccess file and index.php:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
index.php:
<?php
include 'config.php';
$app = new Application();
?>
What I'd like to learn is whether this method has or could have any negative effects in the future in terms of speed and bandwidth. I appreciate your answers and comments.
If the class is there just to wrap you bootstrap stage, then it is pointless. simply have a plain file, which initializes application, load configuration and does all the wiring.
You could also want the index.php file to only contain one line: something that includes file outside DOCUMENT_ROOT. This way, if something goes tits-up with PHP extension, you won't show everyone your DB password and other sensitive details about your code.
As for your current .htaccess setup - no , it will not cause any additional bandwidth usage, but you might think about utilizing browser's cache for image and other assets.
Why redirect and not url rewrite?
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
as for example, this is used by a lot of applications/websites, and you have absolute control of the URL accessed.
And yes, redirecting is another call to the server.
I did some searching around, but I'm not very experienced in PHP, so I don't know the correct Terms/Words I am looking for.
However, I do have a good idea of what I want to do.
I have a website with many images, each image has a database entry (MySQL).
I would like each image to have its own landing page, based on its database values. (Title, Category, Tags, etc...)
I don't want to make a separate .php file on my server for each image. (Eg: pinkflowers.php, redroses.php, etc...)
There should be 1 PHP template file, that outputs the webpage for ALL images, based on the URL (variable) that the user visits.
So if someone visits "mysite.com/pinkflowers.php", the page should be output with the variables of pinkflowers from my database.
However, the file pinkflowers.php doesn't actually exist, only template.php exists, which would be the "blueprint" for all the images in my database.
I would like the .php to be removed from the URL in the browser.
"mysite.com/pinkflowers.php" => "mysite.com/pinkflowers"
I already have code that does this with my existing pages (below); I'm not sure if it will also work with these "imaginary" pages.
(.htaccess)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If the user tries to visit a page that has no related database entry (eg: "mysite.com/430jfif0ij"), they should be re-directed to the homepage.
I'm not expecting anyone to give me ALL the answers, but please at least guide me in the right direction to begin making such a template.
Thanks!
I was able to solve my own question eventually, in the mean time that no Answers were given.
I followed this Tutorial here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
My .htaccess contained:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category1/(.*)$ ./template.php?image=$1
RewriteRule ^category2/(.*)$ ./template.php?image=$1
RewriteRule ^category3/(.*)$ ./template.php?image=$1
etc...
category# - being the "folders" that my images are categorized by.
I did this on the template.php to extract the Variable from the URL:
$result = mysql_query('SELECT * FROM image_list WHERE image_name="' . mysql_real_escape_string($_GET['image']) . '"');
To redirect the visitor to the Homepage if they enter an invalid URL/ID, I used this:
if (mysql_num_rows($result)==0) {
echo "<script>window.location = 'http://mysite.com/'</script>";
}
So the combination of all these methods achieved exactly what I was looking for.
Let your .htaccess be something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ template.php/$1
Then (on template.php) use $_SERVER["path_info"] to determine the path the user has entered (i.e. /pinkflowers) and query the database accordingly.
The reason I like $_SERVER["path_info"] is because it doesn't use (or pollute) the GET variable space.
My root directory contains a selection of PHP files: index.php, about.php etc. Each of these can take an ID parameter to customize a few of the variables on the page, such as contact phone number, email address etc.
If a visitor was to access http://myserver/joebloggs (joebloggs being a parameter, not a subdirectory), I want them to be served up with the index.php page from the root directory, but for joebloggs to be passed to index.php as the ID and the page then customized. I don't want to have to create a subdirectory for every user ID, I'd rather maintain this info in a DB and have PHP generate the pages for me.
Furthermore, when the user then navigates to the about.php page, I would like this ID to be carried over and then have the about page customized too, i.e. a link to 'about' maintains the ID parameter in the URL: http://myserver/joebloggs/about/
I've got a rough idea in my mind how to do this and have been reading up on mod_rewrite but haven't had much luck in piecing various solutions together.
Any pointers or help would be much appreciated.
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/(.+?)\/about
RewriteRule . /about.php?p1=%1 [L]
RewriteCond %{REQUEST_URI} \/.*?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /index.php?p1=$1 [L]
Should do the job. Tested it on my server.
im trying to solve this issue. I have a web site with this simplified struture:
mysite.com/index.php
mysite.com/faq.php
mysite.com/url.php
mysite.com/users/some_content_here
If the user call the index page or the faq page they have to arrive to that pages. BUT if the user writes something like mysite.com/xgsfd (or any other string different than index or faq) they have to call the url.php wich recive the xgsfd string via GET and redirects the user to a particular page.
The url.php script is already done, but i have no idea how to solve the other part, im was thinking using a .htaccess file in the root directory but as you can see it will trigger a infinite loop of rederictions.
Any idea how to solve this issue? Thanks for any help!
You can use rewrite conditions to serve files, folders and symlinks if they actually exist, but otherwise rewrite to the url.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ url.php?url=$1 [QSA,L]
That way requests to faq.php or index.php will be served, while a request to mysite.com/xgsfd will end up rewritten to url.php?url=/xgsfd .