Apache presenting virtual subdirectories and passing parameters to PHP script - php

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.

Related

.htaccess preserve URL but move up a folder

I have a dual language site. If a page isn't found in a localized copy, I need redirect up a level to another folder. Specifically, I need to redirect uk pages to teh corresponding en version.
A sample url is:
http://example.com/uk/this-is-thepage/ to go to http://example.com/this-is-thepage/
The sites have identical page names after the folder.
I don't want to have to redirect every page individually, and the UK site doesn't have many pages at all. I also only want this to happen when the page doesn't exist, as not all pages exist in uk. I wrote some code that was working, but then I realized I couldn't handle an arbitrary /uk/ page.
Inside /uk/.htaccess, you can use this rule to forward to parent directory if requesting URI is not found in `/uk/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ ../$0 [L]
-f and -d conditions mean request is not for a valid file/directory.

Imitate the page creation/url rewriting effect of this strawpoll.me

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?

RewriteRule different pages per user

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

Output Webpage (Variable URL) with Database values, using PHP Template

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.

Mod_Rewrite Query with Root Domain

I'm trying to do a query on some information if someone types, for example:
http://domain.com/abracadabra
If someone enters in this address, I want it to go to:
search.php?query=abracadabra
I've done rewrites with queries before, but not from the root.
Any help would be great!
Would it be possible to check if the string being searched is numerical and send it to a different PHP page? Maybe link to a directory with a new .htaccess?
Doing it from the root is no different than doing it anywhere else. The below will turn every request for a resource (that doesn't exist in the file system) to search.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?query=$1 [L,QSA]

Categories