Jump to content


code structure and removing whitespace

htaccess mod_rewrite apache

  • Please log in to reply
6 replies to this topic

#1 tarquino

  • Members
  • PipPip
  • Member
  • 22 posts

Posted 22 December 2014 - 08:54 AM

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.

#2 requinix

  • Administrators
  • Maddening Administrator
  • 9,725 posts
  • LocationWA

Posted 22 December 2014 - 09:38 AM

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, 22 December 2014 - 09:38 AM.

The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#3 tarquino

  • Members
  • PipPip
  • Member
  • 22 posts

Posted 22 December 2014 - 09:50 AM

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.

#4 Ch0cu3r

  • Moderators
  • Advanced Member
  • 3,418 posts

Posted 22 December 2014 - 03:48 PM

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.

#5 tarquino

  • Members
  • PipPip
  • Member
  • 22 posts

Posted 24 December 2014 - 11:11 AM

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

#6 Ch0cu3r

  • Moderators
  • Advanced Member
  • 3,418 posts

Posted 24 December 2014 - 01:43 PM

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, 24 December 2014 - 01:45 PM.

#7 tarquino

  • Members
  • PipPip
  • Member
  • 22 posts

Posted 26 December 2014 - 11:30 AM

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.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users