Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/261167-creating-dynamic-page-content-and-url/
Share on other sites

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.

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

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.

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.

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

 

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.

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]

 

 

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.