PHP - how to display customized page url in browser - php

I am using Windows IIS 7 with PHP and MySQL.
Whenever someone opens an account on my website, he/she will also create one store ID like MyHawaiiSpa. Once this store ID is generated, it can not be changed. Here I have assumed the pkid of this account is 1.
Now, a visitor clicks the MyHawaiiSpa store link from the home page. The visitor will be sent to shop.php where all items belonging to MyHawaiiSpa will be displayed. By clicking on any product on the MyHawaiiSpa store, the product.php page will open, where product details are displayed.
Currently, I have managed it using querystring in PHP. so each store is distinguished like this: mywebsite.com/user/shop.php?sid=1.
My question is now this: I would like visitor clicks on the MyHawaiiSpa store link from the home page, so that the visitor will see mywebsite.com/user/MyHawaiiSpa/shop.php instead of mywebsite.com/user/shop.php?sid=1 (which I have already done now.)
The solution I was thinking of was this: Whenever someone opens an account and creates their store ID, the folder MyHawaiiSpa will be created and the shop.php as well as the product.php pages will be copied under the MyHawaiiSpa folder programmatically. This process will be repeated for all stores those are going to be created.
Is there any other way to do this so I don't need to create a folder and copy files for each store and still I can get desired result I just explained above?

Well it bad idea to copy PHP file when someone registered, it will be mess to maintain it in long term, you just need to store the ID MyHawaiiSpa as unique key in database. and use store id as slug in URL.
since you are using IIS here is good read to rewrite URL
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/provide-url-rewriting-functionality
You can learn .htaccess mod_rewrite from this link
and then you can covert these rules to IIS7 friendly using this link

You need to use URL rewriting . It is very easy to learn and implement.
On linux servers it is done using a file call .htaccess. On IIS I think file is called
web.config
Basically what this does is, you configure it to show one url while in reality you go to another url.
These links should get you up and running fast
http://www.robbagby.com/php/php-and-iis-running-php-under-fast-cgi-and-url-rewriting/
http://salopek.eu/content/26/introduction-to-url-rewriting-using-iis-url-rewrite-module-and-regular-expressions
https://www.youtube.com/watch?v=guzJWqNJ3DA&feature=channel
http://www.phpgenious.com/2010/04/url-rewriting-with-php-and-iis-7/
http://www.iis.net/learn/application-frameworks/install-and-configure-php-applications-on-iis/provide-url-rewriting-functionality

A good way to do this is an .htaccess rewrite which will allow you to turn your query into an SEO-friendly url and still read properly when called on that page.
Here is apache documentation
A basic example of a rewrite to force all requests through the index page unless they are a real folder or file would be something along the lines of:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.|^$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [NC,QSA,L]
To do a similar rewrite on a windows server you would need to edit a web.config file.

Related

How do I change the url look with .htaccess but keep the php functionality?

I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.

Blog feed/archive pages using PHP/SQL

I have been asked to create a website and wanted to design it so that the user may add new blog posts to a news feed. I've tried this and successfully created a very simple blogging feature which adds/reads entries in a sql database.
However I want to dynamically create new webpages for each blog entry that is added by the user. How would I go about this? I am new to Php/Sql and so I am unsure of where to start. Any pointers in the right direction would be greatly appreciated.
As for an example of what im after, see: http://kingslandprimaryschool.co.uk/
Thanks
The solution is to hire an army of employees, who manually create hundreds of scripts. A lot of work, but what else can you do? Nah, just kidding.
The trick is not to create a new file for every blog page, you just have to change the routing. Start by creating a blogging system which works through consistent URL's, such as blog.php?p=home. When you've finished, create a .htaccess file which routes certain requests to that page. For example, if you would want an URL like my.site/home to load blog.php?p=home you would need the following .htaccess script:
<IfModule mod_rewrite.c>
RewriteEngine On
# Allow existing directories and files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Route the request
RewriteRule ^(.*)$ blog.php\?p=$1 [QSA]
</IfModule>
This would first check whether a file exists at the requested URL. If it does, it shows that file. Otherwise, blog.php?p=myPath is opened. The [QSA] tag after the redirect means that everything after the question mark is passed on to the script you redirect to, so if you would go to my.site/home?day=wednesday the page blog.php?p=home&day=wednesday would be loaded.
If you need more help, feel free to ask any question. Alternatively, you can just Google "htaccess mod_rewrite" or "PHP pretty url" for more information. Hope this was of help!

htaccess for dynamic web pages

I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL
For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm
And so on for them, one more thing do i need to add this for every page? the data is being update on daily basis so is there any easy way to write URL automatically?
Any help highly appreciated.
I don't really think what you are trying to accomplish has anything to do with mysql. What you are looking for is called URL rewriting. There are countless number of articles out there that could show you the direction to follow. I am not very sure which web server you are using right not. I presume it is Apache. Here is Apache module_rewrite guide.
Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely.
So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page.
So in htaccess, something like...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]
Then in controller.php you parse the url and load the correct page.
A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this:
http://www.mywebsite.com/flights/10/Africa/Entebbe.htm
This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests).
So in htaccess...
RewriteRule
^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
flights-result.php?id=$1&country=$2&place=$3 [L]
Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not.
Remember to change all the links your app outputs to the new style as well.
You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :)

How to change appearance of URL from within a PHP script

I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.
Here is an example of what I would like to do: lets say some page id = 1 and the title of that page is HelloWorld.
Instead of site.com/page.php?id=1 I would like the url to appear as site.com/HelloWorld
But the problem I am having is that I only get to know the title of the page inside that page after I query for the title by id.
Considering the setup I described, is there a way to make the urls appear as the names of the pages? And also, if someone links to that page by using the better looking url with the name of the page instead of the id, is there still a way to get the id and by that, the rest of the page contents?
Thanks!!
What you need is to learn more on how .htaccess work.
Here is a good link that got me started:
.htaccess tricks and tips
Update:
Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
</IfModule>
This is actually a function of your HTTP server software (often Apache), not of PHP.
What you're seeing happen on sites with "friendly" URLs is that the friendly name is captured in a Regular Expression and then passed to PHP.
For example:
GET /HelloWorld
is sent to your web server.. the web server parses it
RewriteCond ^(A REGULAR EXPRESSION TO CAPTURE YOUR FRIENDLY NAME)$
RewriteRule page.php?id=(EXPRESSION FROM ABOVE)
In this way your PHP script will always receive the friendly name as a parameter.
Take a look at "mod_write" for Apache - which you can often create rules for using an ".htaccess" file in the root directory.

htaccess redirect question

Ok, so my htaccess file first rewrites any url to include the full address. I needed this because facebook is picky about the urls coming through for the application ID.
Now, this site I'm building is a business profile site of sorts, built with PHP and MYSQL. The profile page can query based on either the id number, or the unique slug id, which acts as an easy-to-read URL for the businesses to use when they want people to link to their profile. So I have the rewrite engine putting the string into a variable in the URL so the profile page can get the string and use it to query. As of right now, it's going to rewrite any URL or file that doesn't exist and send it to the profile page. This seems a little sloppy, as I wouldn't want a user who enters an incorrect URL for, say, the contact page, and instead get's sent to the invalid profile page. Here's the rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* profile.php?uniqueID=$0 [L]
Now, I'm thinking the only real way I could do this instead is to make the urls more unique, so instead of www.bizprofiles.com/uniqueprofilename as the basic structure, I do www.bizprofiles.com/profils/uniqueprofilename and instead write the condition to exact match the url up until the unique name.
I'm wondering if anyone has any better of an idea?
.htaccess can't check you business logic, e.g. if a profile exists. You will have to check within profile.php and redirect appropriately like:
header('HTTP/1.0 404 Not Found');
header('Location: /your404File.php');
exit;
The second structure www.bizprofiles.com/profils/uniqueprofilename (I think there's a spelling mistake, should be profiles or profile) would be more readable in my opinion and gives you the opportunity to extend the site with different .htaccess rules at some point.

Categories