elchenuk Posted April 18, 2012 Share Posted April 18, 2012 Hi all, I'm not a seasoned PHP developer but learning all the time! However I am stumped at creating a solution that can do the following: I have a list of names of people (first name and surname) that I have in a CSV file. I know roughly how to read in data from a CSV file which is great as this means that I can customise the greeting of a personalised web page. What I am struggling with is to create personal URLs also. So for example I want to be able to create a personal URL such as www.domain.com/firstnamesurname/ and then this page I can add a personalised greetings etc. Is this possible? Any advice would be greatly appreciated. eddy Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 yes it's possible. You could use .htaccess to rewrite the URL so when someone accesses for example www.example.com/AyKay47 the server will actually serve them www.example.com/index.php?username=aykay47 and handle the query string accordingly to serve the correct data in correlation to that user. Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 18, 2012 Author Share Posted April 18, 2012 Of course, perfect! I will that a go, dont suppose you can recommend a good resource that I can read? Quote Link to comment Share on other sites More sharing options...
joecooper Posted April 18, 2012 Share Posted April 18, 2012 Thats what i would suggest too. I will look for some htaccess example code. Quote Link to comment Share on other sites More sharing options...
joecooper Posted April 18, 2012 Share Posted April 18, 2012 RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1 anyone going to domain.com/bob will actually load domain.com/user.php?username=bob stick that in a file called .htaccess Windows will not let you name files like this, so use notepad to do it. Also do you need help in the actual PHP side of it? if so, post your script your using and we can take a look. will be something like: $username = $_GET['username']; to grab the username from the URL Quote Link to comment Share on other sites More sharing options...
trq Posted April 18, 2012 Share Posted April 18, 2012 this should send anyone goingto domain.com/user.php?username=bob to domain.com/bob and display the same page This is in fact the complete opposite of what will happen given the rules above. Quote Link to comment Share on other sites More sharing options...
joecooper Posted April 18, 2012 Share Posted April 18, 2012 Ah i thought as much. The page i got it from said it wrong. So the correct way would be: RewriteEngine On RewriteRule user.php?username=$1 ^([a-zA-Z0-9_-]+)$ RewriteRule user.php?username=$1 ^([a-zA-Z0-9_-]+)/$ ?? Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 18, 2012 Author Share Posted April 18, 2012 Great thanks, is it possible to get the info for the rewrite from a CSV file? i.e. Name, new URL, old url Joe Bloggs, domain.com/joe domain.com/user.php?username=Joe Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 Ah i thought as much. The page i got it from said it wrong. So the correct way would be: RewriteEngine On RewriteRule user.php?username=$1 ^([a-zA-Z0-9_-]+)$ RewriteRule user.php?username=$1 ^([a-zA-Z0-9_-]+)/$ ?? no, the actual rewrite was correct the first time, you just stated what it would do incorrectly. Please do not post responses unless you are sure of what you are talking about. It can be more detrimental then helpful. OP, there are many resources on the subject, you will want to google "mod_rewrite" or "rewrite rule" and you will get an idea of how to go about this. Quote Link to comment Share on other sites More sharing options...
joecooper Posted April 18, 2012 Share Posted April 18, 2012 sorry AyKay47, Its what another site posted (about.com), which was incorrect. Please dont knock people down for trying to help. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 sorry AyKay47, Its what another site posted (about.com), which was incorrect. Please dont knock people down for trying to help. I wasn't trying to knock you down, everyone makes mistakes. But you must ask yourself if the answer will help or hurt the person asking. Sometimes it is better to not say anything at all. Quote Link to comment Share on other sites More sharing options...
joecooper Posted April 18, 2012 Share Posted April 18, 2012 Well I assumed it would help. Maybe its best not to help anyone at all which now i wont. Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 18, 2012 Author Share Posted April 18, 2012 Guys I appreciate all your help. So can I rewrite a URL using: www.domain.com/keyword1keyword2 to www.domain.com/user.php?username=keyword1-keyword2 Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 18, 2012 Author Share Posted April 18, 2012 Just to add more context to my question and to make it more complex! I have a big list of company names i.e. acme company, red widgets ltd and I want to send out a mailshot with URLs as www.domain.com/acmecompany www.domain.com/redwidgetsltd ...etc Can I grab these specific keywords and rewrite them so that I can have something like www.domain.com/user.php?company=acme-company And consequently I am then able to use that variable within my page copy? I can't get my head round the situation when there are a lot of words in a company name AND how do I separate words in the company name?? eddy Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 18, 2012 Share Posted April 18, 2012 Just to add more context to my question and to make it more complex! I have a big list of company names i.e. acme company, red widgets ltd and I want to send out a mailshot with URLs as www.domain.com/acmecompany www.domain.com/redwidgetsltd ...etc Can I grab these specific keywords and rewrite them so that I can have something like www.domain.com/user.php?company=acme-company And consequently I am then able to use that variable within my page copy? I can't get my head round the situation when there are a lot of words in a company name AND how do I separate words in the company name?? eddy Yes you can do that, it all wraps around regular expressions. Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 18, 2012 Author Share Posted April 18, 2012 OK thanks AyKay47, I have created two simple rewrites just to test the page content and the page is loading but the variables are not being picked up on the page. This is the code I use for the page to grab whats in the var part of the URL <?php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } ?> <?php $url = curPageURL(); parse_str(parse_url($url, PHP_URL_QUERY), $vars); $name = $vars['var']; $customer = str_replace("-"," ",$name); ?> And then I use this to output: <?php echo "Dear $customer\n"; This works fine if I manually type the url www.domain.com/folder1/page.php?var=Firstname-Surname BUT doesnt work when I do the rewrite: RewriteRule ^firstnamesurname$ folder1/page.php?var=Firstname-Surname [NC,L] Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted April 19, 2012 Share Posted April 19, 2012 post the entire rewrite, including any conditions and the engine start. Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 19, 2012 Author Share Posted April 19, 2012 Not sure if this what you wanted but here is everything that is in the .htaccess file # Follow symbolic links in this directory. Options +FollowSymLinks # Make Drupal handle any 404 errors. ErrorDocument 404 /index.php # Force simple error message for requests for non-existent favicon.ico. <Files favicon.ico> # There is no end quote below, for compatibility with Apache 1.3. ErrorDocument 404 "The requested file favicon.ico was not found. </Files> # rewrite for the mdg transformation pages RewriteEngine on RewriteRule ^anothercompany$ mgd-trans-campaign/index.php?var=Another-Company [NC,L] RewriteRule ^birkdalehighschool$ mgd-trans-campaign/index.php?var=Birkdale-High-School # Set the default handler. DirectoryIndex index.php # Override PHP settings. More in sites/default/settings.php # but the following cannot be changed at runtime. # PHP 4, Apache 1. <IfModule mod_php4.c> php_value magic_quotes_gpc 0 php_value register_globals 0 php_value session.auto_start 0 php_value mbstring.http_input pass php_value mbstring.http_output pass php_value mbstring.encoding_translation 0 </IfModule> # PHP 4, Apache 2. <IfModule sapi_apache2.c> php_value magic_quotes_gpc 0 php_value register_globals 0 php_value session.auto_start 0 php_value mbstring.http_input pass php_value mbstring.http_output pass php_value mbstring.encoding_translation 0 </IfModule> # PHP 5, Apache 1 and 2. <IfModule mod_php5.c> php_value magic_quotes_gpc 0 php_value register_globals 0 php_value session.auto_start 0 php_value mbstring.http_input pass php_value mbstring.http_output pass php_value mbstring.encoding_translation 0 </IfModule> # Requires mod_expires to be enabled. <IfModule mod_expires.c> # Enable expirations. ExpiresActive On # Cache all files for 2 weeks after access (A). ExpiresDefault A1209600 <FilesMatch \.php$> # Do not allow PHP scripts to be cached unless they explicitly send cache # headers themselves. Otherwise all scripts would have to overwrite the # headers set by mod_expires if they want another caching behavior. This may # fail if an error occurs early in the bootstrap process, and it may cause # problems if a non-Drupal PHP file is installed in a subdirectory. ExpiresActive Off </FilesMatch> </IfModule> # Various rewrite rules. <IfModule mod_rewrite.c> RewriteEngine on # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redirect users to your preferred # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: # # To redirect all users to access the site WITH the 'www.' prefix, # (http://example.com/... will be redirected to http://www.example.com/...) # adapt and uncomment the following: RewriteCond %{HTTP_HOST} ^freedomcomms\.com$ [NC] RewriteRule ^(.*)$ http://www.freedomcomms.com/$1 [L,R=301] # # To redirect all users to access the site WITHOUT the 'www.' prefix, # (http://www.example.com/... will be redirected to http://example.com/...) # uncomment and adapt the following: # RewriteCond %{HTTP_HOST} ^www\.freedomcomms\.com$ [NC] # RewriteRule ^(.*)$ http://freedomcomms.com/$1 [L,R=301] # Modify the RewriteBase if you are using Drupal in a subdirectory or in a # VirtualDocumentRoot and the rewrite rules are not working properly. # For example if your site is at http://example.com/drupal uncomment and # modify the following line: # RewriteBase /drupal # # If your site is running in a VirtualDocumentRoot at http://example.com/, # uncomment the following line: # RewriteBase / # Rewrite URLs of the form 'x' to the form 'index.php?q=x'. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] </IfModule> The bit that I have added to the file was # rewrite for the mdg transformation pages RewriteEngine on RewriteRule ^anothercompany$ mgd-trans-campaign/index.php?var=Another-Company [NC,L] RewriteRule ^birkdalehighschool$ mgd-trans-campaign/index.php?var=Birkdale-High-School hope this helps Quote Link to comment Share on other sites More sharing options...
elchenuk Posted April 19, 2012 Author Share Posted April 19, 2012 Ok I have just done an echo of the URL and it is showing as the requested URL and not as the substituted URL. And of course it is the substituted URL that contains the variables that will be entered into the page copy. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.