Jump to content
Sign in to follow this  

code structure and removing whitespace

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-_]+)$ 


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

Share this post

Link to post
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

Share this post

Link to post
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.

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

so to this code if i add \%20 would it replace the whitespace with a - or do i need to do something else?

RewriteRule ^/user/([a-z]+)/cake/([a-z\%20A-Z0-9-_]+)$  /cakes.php?userid=$1&cakebaked=$2

Share this post

Link to post
Share on other sites

No! requinix already explained mod rewrite cant do that that (in reply #2)


The substitution needs to take place in your PHP code. The place you need to edit is where it outputs the urls to yoursite.com/user/.../cake/...

Edited by Ch0cu3r

Share this post

Link to post
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

$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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


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.