Forum Moderators: phranque
First of all, i´ve seen some comparable problems in this forum, but because im an absolute beginner I couldnt duplicate the solutions to the following problems. Hope you guyz can help me step by step :D .
I got the following working with some help:
Code .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule (.*)/(.*)/(.*)/ product.php?categorie=$1&subcategorie=$2&slug=$3
When I now link to:
mysite.com/hat/red hat/super hat/
The right page and info turns up dynamically (Huray! )
This still leaves me with 4 problems:
1. Images wont show up aymore on the speific product.php page. What should I add or change in the .htaccess for this?
2. The URL still contains %20 (spaces) which I want to convert to dashes. When I manually link with dashes it does work, but there is no way to do this automatcally now because the variables in my database do not contain hyphens.
I now link to each page using this URL:
<a href="http://www.mysite.com/<?php echo $row_Recordset1['categorie'] . "/" . $row_Recordset1['subcategorie'] . "/" . $row_Recordset1['slug'] . "/"; ?>
So how do I rewrite the URL with dashes?
3. How do I rewrite the above given URL so that it automatically replaces dashes with hyphens (even when I link with a link that does not contain hyphens)? I know a 301 is probably involved...but thats about it :).
4. I will create the categorie and subcategorie pages manually. E.g:
hat.php
hat/black-hat.php
How can I rewrite ".php" to "/" ? This is something that should be done for all categories except for 1 dynamic page for user searches where I use a "get", and it should not clash with therewritten product.php page.
Hope you can help!
[edited by: jdMorgan at 3:57 pm (utc) on Oct. 17, 2008]
[edit reason] Please use example.com only. [/edit]
I kind of see what you did in the code and how it works (however I wouldn't have been able to write this out).
I still want to make one addition, everything works absolutely proper, however, /anypage/anysubpage.php isnt rewritten to /anypage/anysubpage/ like lower levels are. I think I need to:
# Externally redirect direct client requests for root-directory .php URLs to extensionless static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^/.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^([^/.]+)\.php$ http://www.example.com/$1/? [R=301,L]
But i'm not sure. Should I add a code something like:
# Externally redirect direct client requests for root-directory .php URLs to extensionless static URLs in subdirectory level 1
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^/.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^([^/.]+)/([^/.]+)\.php$ http://www.example.com/([^/]+)/$1/? [R=301,L]
Just writing what seems logical to me..
The change you need is highlighted below:
# Enable mod_rewrite module
Options +FollowSymLinks
# Enable rewrite engine
RewriteEngine on
#
# Externally redirect direct client requests for Product.php URLs to extensionless static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /Product\.php\?categorie=([^&]+)&subcategorie=([^&]+)&slug=([^\ ]+)\ HTTP/
RewriteRule ^Product\.php$ http://www.example.com/%1/%2/%3/? [R=301,L]
#
# Externally redirect direct client requests for .php URLs to extensionless static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[b]([^/]+/)*[/b][^/.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^([b]([^/]+/)*[/b][^/.]+)\.php$ http://www.example.com/$1/? [R=301,L]
#
# Externally redirect non-canonical hostname requests to canonical URL
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# If requested extensionless URL resolves to an existing .php file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# internally rewrite the requested URL to the .php filepath
RewriteRule ^(([^/]+/)*[^./]+)/?$ /$1.php [L]
#
# Internally rewrite static product page URL requests to Product.php script filepath
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /Product.php?categorie=$1&subcategorie=$2&slug=$3 [L]
# Externally redirect direct client requests for root-directory non-extension URLs to extensionless static URLs ending with /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^/.]+\(\?[^\ ]*)?\ HTTP/
RewriteRule ^([^/.]+)\$ http://www.example.com/$1/? [R=301,L]
I just got rid of the .php stuff and want to add this in addition, or is this not possible?
Also I'm in a little bit of a pickle. I have a page called Personaldata.php on my server where people fill in their personal information. If a field is not filled with their information, an "errormeassage appears". However, when the rewrite for all .php files is on, this error message doesnt work:
<?php if(isset($_POST['submit']))
{
//input not yet approved
$_SESSION['checkedInput'] = 0;
//filter unwanted expressions
filterInput($_POST['f_voornaam']);
filterInput($_POST['f_achternaam']);
filterInput($_POST['f_straatnaam']);
filterInput($_POST['f_huisnummer']);
filterInput($_POST['f_postcode']);
filterInput($_POST['f_woonplaats']);
filterInput($_POST['f_land']);
filterInput($_POST['emailadres']);
filterInput($_POST['b_voornaam']);
filterInput($_POST['b_achternaam']);
filterInput($_POST['b_straatnaam']);
filterInput($_POST['b_huisnummer']);
filterInput($_POST['b_postcode']);
filterInput($_POST['b_woonplaats']);
filterInput($_POST['b_land']);
filterInput($_POST['betaalmehode']);
filterInput($_POST['nieuwsbrief']);
//error check billing adres
$errormessage = "";
if (strlen($_POST['f_voornaam'])==0 ¦¦ strlen($_POST['f_voornaam'])>50)
$errormessage .= "<li>Vul een geldige voornaam in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['f_achternaam'])==0 ¦¦ strlen($_POST['f_achternaam'])>50)
$errormessage .= "<li>Vul een geldige achternaam in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['f_straatnaam'])==0 ¦¦ strlen($_POST['f_straatnaam'])>50)
$errormessage .= "<li>Vul een geldige straatnaam in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['f_huisnummer'])==0 ¦¦ strlen($_POST['f_huisnummer'])>10)
$errormessage .= "<li>Vul een geldig huisnummer in (min 1 / max 10 karakters)</li>";
if (strlen($_POST['f_postcode'])==0 ¦¦ strlen($_POST['f_postcode'])>10)
$errormessage .= "<li>Vul een geldig postcode in</li>";
if (strlen($_POST['f_woonplaats'])==0 ¦¦ strlen($_POST['f_woonplaats'])>50)
$errormessage .= "<li>Vul een geldige woonplaats in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['emailadres'])==0 ¦¦ strlen($_POST['emailadres'])>50 ¦¦ !validate_email($_POST['emailadres']))
$errormessage .= "<li>Vul een geldig e-mail adres in (min 1 / max 50 karakters)</li>";
//error check shipping adres if different from billing adres.
if ($_POST['gebruikBezorgadres'] == 1)
{
if (strlen($_POST['b_voornaam'])==0 ¦¦ strlen($_POST['b_voornaam'])>50)
$errormessage .= "<li>Vul een geldige voornaam (bezorgadres) in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['b_achternaam'])==0 ¦¦ strlen($_POST['b_achternaam'])>50)
$errormessage .= "<li>Vul een geldige achternaam (bezorgadres) in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['b_straatnaam'])==0 ¦¦ strlen($_POST['b_straatnaam'])>50)
$errormessage .= "<li>Vul een geldige straatnaam (bezorgadres) in (min 1 / max 50 karakters)</li>";
if (strlen($_POST['b_huisnummer'])==0 ¦¦ strlen($_POST['b_huisnummer'])>10)
$errormessage .= "<li>Vul een geldig huisnummer (bezorgadres) in (min 1 / max 10 karakters)</li>";
if (strlen($_POST['b_postcode'])==0 ¦¦ strlen($_POST['b_postcode'])>10)
$errormessage .= "<li>Vul een geldig postcode (bezorgadres) in</li>";
if (strlen($_POST['b_woonplaats'])==0 ¦¦ strlen($_POST['b_woonplaats'])>50)
$errormessage .= "<li>Vul een geldige woonplaats (bezorgadres) in (min 1 / max 50 karakters)</li>";
}
//input everything in session variables
$_SESSION['f_voornaam'] = $_POST['f_voornaam'];
$_SESSION['f_achternaam'] = $_POST['f_achternaam'];
$_SESSION['f_straatnaam'] = $_POST['f_straatnaam'];
$_SESSION['f_huisnummer'] = $_POST['f_huisnummer'];
$_SESSION['f_postcode'] = $_POST['f_postcode'];
$_SESSION['f_woonplaats'] = $_POST['f_woonplaats'];
$_SESSION['f_land'] = $_POST['f_land'];
$_SESSION['emailadres'] = $_POST['emailadres'];
$_SESSION['b_voornaam'] = $_POST['b_voornaam'];
$_SESSION['b_achternaam'] = $_POST['b_achternaam'];
$_SESSION['b_straatnaam'] = $_POST['b_straatnaam'];
$_SESSION['b_huisnummer'] = $_POST['b_huisnummer'];
$_SESSION['b_postcode'] = $_POST['b_postcode'];
$_SESSION['b_woonplaats'] = $_POST['b_woonplaats'];
$_SESSION['b_land'] = $_POST['b_land'];
$_SESSION['betaalmethode'] = $_POST['betaalmethode'];
$_SESSION['nieuwsbrief'] = $_POST['nieuwsbrief'];
$_SESSION['gebruikBezorgadres'] = $_POST['gebruikBezorgadres'];
if (strlen($errormessage) == 0)
{
//inserten dummy orderid
mysql_select_db($database_sosql, $sosql);
$query_Recordset3 = "INSERT INTO persoonlijkegegevensorders (OrderID ,OrderSubtotaal ,OrderVerzendkosten ,OrderTotaalprijs ,Achternaam ,Straatnaam ,Huisnummer ,Postcode ,Woonplaats ,Land ,Emailadres ,Apartbezorgadres ,ApartbezorgadresVoornaam ,ApartbezorgadresAchternaam ,ApartbezorgadresStraatnaam ,ApartbezorgadresHuisnummer ,ApartbezorgadresPostcode ,ApartbezorgadresWoonplaats ,ApartbezorgadresLand ,Betaalmethode ,Nieuwsbrief ,Orderdatum ,Orderstatus) VALUES (NULL , '0', '0', '0', '', '', '0', '0', '', '', '', '0', '', '', '', '', '', '', '', '', NULL , '0000-00-00', NULL)";
$Recordset3 = mysql_query($query_Recordset3, $sosql) or die(mysql_error());
$_SESSION['orderid'] = mysql_insert_id($sosql);
$_SESSION['checkedInput'] = 1;
if (($_SESSION['betaalmethode']) == "iDeal")
{
header('Location: /Afrekenenideal/');
exit;
}
if (($_SESSION['betaalmethode']) == "Zelfstandige bankoverboeking")
{
header('Location: /Afrekenenoverschrijven/');
exit;
}
}
else
{
$_SESSION['checkedInput'] = 0;
$errormessage = "De volgende velden zijn niet correct ingevoerd:<br /><br />".$errormessage;
}
//there are errors, display initial form with values and errors
}
The error messages are show with: <?php echo $errormessage; ?> . Would it be possible to do an exception in the .htaccess for this specific file so that it can show the error messages, or is there a better simple php solution for this? Searched the web for both, but couldnt find a solution... Also, the submit button wont send me to the next page like it does when the rewrites are turned off...
[edited by: Joppiesaus at 10:51 am (utc) on Nov. 7, 2008]
So, if I also want to redirect /anypage to /anypage/ or /anypage/subpage to /anypage/subpage/ is this code beneath the right one to use:RewriteCond %{REQUEST_FILENAME}
# Externally redirect direct client requests for root-directory non-extension URLs to extensionless static URLs ending with /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^/.]+\(\?[^\ ]*)?\ HTTP/
RewriteRule ^([^/.]+)\$ http://www.example.com/$1/? [R=301,L]
No, you need to incorporate logic to avoid redirecting slashless URLs which should not be redirected. This is similar to the internal extensionless-URL-to-PHP rewrite rule, but uses an external redirect to "correct" the slashless URLs:
# If slashless extensionless URL will resolve to an existing .php file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# externally redirect the requested URL to add a trailing slash
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [R=301,L]
I don't have time to read long scripts, and that code is likely not relevant. What is needed is the URL of the php error page, which you can exclude from the internal extensionless-URL-to-PHP rewrite and direct-client-php-file-request redirect rules using a negative-match RewriteCond:
RewriteCond %{REQUEST_URI} !^/URL-path-to-your-php-error-page$
# Negative rewrite to show Persoonlijkegegevens.php
RewriteCond %{REQUEST_URI} !^/Persoonlijkegegevens.php$
It doesnt seem to work dough, the page still only reloads itself (/Persoonlijkegegevens/), doesnt show the error messages (it only does this when it is allowed to go to .php) and it doesnt redirect to one of either followup pages like normally (also just reloads to /Persoonlijkegegevens/
I tried positioning the code in different parts of the .htaccess without luck. Could there be some kind of conflict between the php code and the htaccess rule?
Is this (or is there one available) also a peace of code which can simply exclude URL's from the .htaccess fil all together? There are like 4 pages in my website which wont be indexed or anything that are used by payment systems and so on for secure payment. If they use rewrites there it wont work. Sois here a way to exclude these 4 pages from the entire file? Thanks.
[edited by: Joppiesaus at 2:45 pm (utc) on Nov. 7, 2008]
If you pursue mod_rewrite and .htaccess functions in-depth, you will find many functions that should or must be applied to all requests. A few examples are:
Do not try to take "the easy way" in server configuration. Take the technically-correct way instead.
Jim