tarquino Posted December 22, 2014 Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/ Share on other sites More sharing options...
requinix Posted December 22, 2014 Share Posted December 22, 2014 (edited) 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 December 22, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500353 Share on other sites More sharing options...
tarquino Posted December 22, 2014 Author Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500354 Share on other sites More sharing options...
Ch0cu3r Posted December 22, 2014 Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500383 Share on other sites More sharing options...
tarquino Posted December 24, 2014 Author Share Posted December 24, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500558 Share on other sites More sharing options...
Ch0cu3r Posted December 24, 2014 Share Posted December 24, 2014 (edited) 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 December 24, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500566 Share on other sites More sharing options...
tarquino Posted December 26, 2014 Author Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293231-code-structure-and-removing-whitespace/#findComment-1500681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.