Jump to content

code structure and removing whitespace


tarquino

Recommended Posts

hello everyone.

 

could you advise me on the structure of my code to check if it is correct:

RewriteRule ^/user/([a-z]+)/cake/([a-z\ .*A-Zx0-9-_]+)$ 

/cakes.php?userid=$1&cakebaked=$2

what i am trying to do is to add a - instead of a whitespace which appears in the url as %20.

Link to comment
Share on other sites

RewriteRule (and mod_rewrite) cannot make substitutions*. If you don't want spaces then you need to make sure your code doesn't output URLs with spaces.

 

As for accepting hyphens in place of spaces, (1) yes it does that but (2) it still accepts spaces so you'll want to remove that part. You also have a stray "x" in there doing nothing, and unless you want to literally accept a period and an asterisk then then ".*" shouldn't be there either.

 

* Easily.

Edited by requinix
Link to comment
Share on other sites

thanks requinix for explaining. i am new to the rewriterules hence i did not know what it really meant. i put the code together after reading for the various functions and trying to implement it for my instance. do you know some place which would help me learn the htaccess code better?

i am really interested in learning exactly what is in between the parenthesis. if you could point me in the right direction or provide me what it means that would be helpful.

Link to comment
Share on other sites

Before you start using mod rewrite you need to get yourself familiar with regular expressions (regex for short) as this is what is used for the rewriteRules. regular-expressions.info is a good resource for learning regex

 

As for learning mod rewrite I recommend you looking at Apaches documentation first. When I first starting using mod rewrite I used this guide here.

Link to comment
Share on other sites

i have the php (linkchange.php) which translates the array into lowercase and with a hypen, however i am unable to get it working properly. the code is the following:

 

the code in the htaccess



RewriteRule ^/user/.*[A-Z\ ] linkchange.php?l=%{REQUEST_URI} [L]
RewriteRule ^/user/([a-z]+)/baked/([a-z.*0-9_-]+)$ cakes.php?id=$1&baked=$2


 

for the cakes.php



<?php 
$cakesmade = array();
$cakesmade = array(
"kate" => array(
"cake" => array(
"cakeingredients" => "egg, flour"),
"Lovely Chocolate Cake" => array(
"cakeingredients" => "chocolate, eggs, flour"),
"amazing cake" => array(
"cakeingredients" => "lemons, flour")
),
);




?>
<?php foreach($cakesmade as $id => $donecake) 
foreach($donecake as $bakedcake => $description)
 { echo "<a href='user/$id/baked/{$bakedcake}'>{$bakedcake}</a><br>";
}
?>


// this part should appear once the user and variable are set in the url
The selected cake is: <?php if(isset($bakedcake)) { echo $bakedcake; } else { echo "not set"; } ?> by <?php if(isset($id)) { echo $id; }  else { echo "not set"; } ?>.


 

for the linkchange.php

 



<?
    $link = $_GET['l'];
    $newlink = strtr($link," ABCDEFGHIJKLMNOPQRSTUVWXYZ","-abcdefghijklmnopqrstuvwxyz");
    header("Location: http://www.example.com". $newlink,TRUE,301); exit;
?>


 

the linkchange.php is the code which makes the substitution into lowercase and hyphen.

are the above codes formatted correctly? and where exactly will i need to make the changes? please help i am eager to learn this.
Link to comment
Share on other sites

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.