htaccess redirect question - php

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.

Related

PHP - how to display customized page url in browser

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.

Automatically Create Custom Page (or URL) for New Site User - PHP

My website is, in its most basic form, a network site for a specific type of individuals. When an individual registers, I would like to create a custom page (or at least the image/facade of one) displaying their profile. Currently, I am using PHP $_GET parameters to determine which profile to display when a user navigates to the respective script on the site.
Aiming for better SEO, I would like each user to have their own unique link; for example, FaceBook allows its users to have a custom URL which they can link. How can I automatically generate a page within a directory for each new user that registers, and have that page automatically update when the user updates his/her details? I understand that there may be a potential process with .htaccess, using the user's name/title/etc to pull their details from the database but displaying it as a unique URL.
Can anyone provide suggestions or examples of implementations of this? I appreciate any and all help!
P.S. I imagine that sorting the files based off of the user's location, i.e. by state, should not be that difficult once the aforementioned functionality has been sorted out. I would ideally like a directory structure such as this ../users/state/user_name, where state is the user's location, and user_name is their unique name. Thanks again!
There is little work with apache's .htaccess - most of the logic is in the php itself. Apache just redirects every request to an chosen script file.
I assume you use apache wid mod_rewrite. If that's the case you should add this to .htaccess file:
# check if mod_rewrite is present
<IfModule mod_rewrite.c>
#turns it on
RewriteEngine on
#if the requested url isn't a file or a dir
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#process index.php, no matter what was in the url
RewriteRule ^ index.php [L]
</IfModule>
(note that .htaccess must also have permissions to overwrite the apache's current settings)
in the index.php you can check what url was sequested and process the info
$whatTheUserRequested=$_SERVER['REQUEST_URI'];
$parameterArray=explode('/', $whatTheUserRequested);
//check the first param.
switch($parameterArray[1])
{
//if it was something like "thesite.com/main/fsadf/hgfdsgsdf/...."
case "main":
include "mainPage.php";
//....or do something else
break;
//if it was something like "thesite.com/login/asdfe/xxxx/...."
case "login":
include "loginPage.php";
//....or do something else
break;
default:
//here you MUST check if $parameterArray[1] is a username
if(check in db if $parameterArray[1] is user)
{
include "userPage.php";
}
else
{
include "pageNotFound.html"
};
}
You may check also for the 3rd or 4th parameter in the included php's, to process url's like "mysite.com/johnny95/edit"
You can generate the unique url based on any logic you wish. Use .htaccess to redirect sending a parameter which can queried in your database. You can find info on how here. I would stay away from directory structure based on state - people move and moving directories seems not worth the effort. Stay with a flat directory structure for simplicity
If you look at this post's url: questions/23275736/automatically-create-custom-page-or-url-for-new-site-user-php they are including "questions" (probably a database table), "23275736" (probably the table key ID) and a user friendly string. I don't know for sure, but I suspect they redirect using .htaccess in a method similar to what is in the attached link.
You can also look at this post's answer for another example which uses a redirect with a slash.

conditional .htaccess redirect - redirect to two different pages depending on the url

My website functions a little like 9gag or damnlol, etc.
when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.
Here is the Rule:
RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1
What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.
I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.
Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:
Usernames can contain numbers, but not only numbers
Username URLs cannot contain '-'
Item IDs cannot contain alphabets
URLs 104-funny, 104- and 104, all work for view.php
Thank you for your help in advanced!
#article urls
RewriteRule ^([0-9]+)(-.*)?$ view.php?id=$1
#profile urls.
RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1

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 :)

htaccess rule to add locale prefix to URL after locale cookie has been set

I have successfully set up localisation on my website using php gettext, and a browser cookie is set when a visitor chooses their preferred language.
Whilst the pages are shown in the correct language, I want the URL to reflect the currently selected language by prefixing the URL with the language code (not the locale code).
This is what my htaccess file looks like at the moment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en(/?$|/(.*)) $1?locale=en_GB [QSA,DPI,L]
RewriteRule ^fr(/?$|/(.*)) $1?locale=fr_FR [QSA,DPI,L]
RewriteRule ^de(/?$|/(.*)) $1?locale=de_DE [QSA,DPI,L]
RewriteRule ^es(/?$|/(.*)) $1?locale=es_ES [QSA,DPI,L]
RewriteRule ^ru(/?$|/(.*)) $1?locale=ru_RU [QSA,DPI,L]
Let's say a visitor clicks on the French language link on the homepage, the URL displayed to the visitor is mysite.com/fr and the language changes to French.
If they then click on the link for page1.php the URL changes to mysite.com/page1.php and the French version of Page 1 is displayed.
I would like the URL to be shown as mysite.com/fr/page1.php.
I've tried adding the following lines to htaccess, just below the RewriteCond lines, but I'm clearly doing it wrong:
RewriteCond %{HTTP_COOKIE} locale=([a-z]{2}\_[A-Z]{2}) [NC]
RewriteRule ^(.*)$ /%1/$1 [NC,L,QSA]
Note that in the lines above, I am attempting to prefix the URL with the entire locale code (e.g. fr_FR) whereas ultimately what I want is to prefix the URL with just the first two characters of the locale code (e.g. fr).
Thank you in advance.
I don't think you should use .htaccess rewrites for this ..
If a user comes to your site with the URL:
http://mysite.com/page1.php you should figure you what language you want to show to him and redirect him, for instance to: http://mysite.com/en/page1.php - by looking at the browser language accept value for instance.
If a user comes to your site with a URL like:
http://mysite.com/en/page1.php then you don't have to do anything, you know which language to use.
I can strongly recommend you don't use cookies for this. Many sites use cookies for this for absolutely no purpose at all, all they do is send you to the page in whatever language you selected. Completely infuriating if you don't accept cookies by default (which everyone should).
Keep in mind that all your links on your site will always have the language as part of their URL, because if they don't you're back to situation 1 and your user might get switched back to the default language. Using a cookie here will only complicate matters, what if you really did want to switch to the French site after using the English site for a bit?
I guess the bottom line is: don't use cookies in combination with your URL to identify a resource, it becomes very vague which resource exactly you're referring to because cookies are not obvious. The only exception is of course when you're doing some authentication stuff like user accounts and whatnot, but then lack of a cookie should always result in a 'please log in', not something that looks like the thing you wanted, but isn't.

Categories