2

My first blast at mod rewrite. I have this horrible link below whihc takes you to a profile page. On the profile page I fetch the user based on the i parameter.

    <a href="engineer-profile.php?country=<?php echo str_replace(' ','-',strtolower($row['country']));?>&area=<?php echo str_replace(' ','-',strtolower($row['area'])); ?>&i=<?php echo $row['id'];?>&name=<?php echo str_replace(' ','-',strtolower($row['name']));?> " class="btn btn-md btn-bitbucket"><i class="fa fa-user"></i> View Profile</a>

I've been trying to make a mod rewrite so the link looks like follows

/uk/southport/23/company-name/

so far I have this in my .htaccess file

  RewriteEngine on
  RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ engineer-profile.php?country=$1&area=$2&i=$3&name=$4 [L]

now if I restructure my link so it looks as follows:

   <a href="/<?php echo str_replace(' ','-',strtolower($row['country']));?>/<?php echo str_replace(' ','-',strtolower($row['area'])); ?>/<?php echo $row['id'];?>/<?php echo str_replace(' ','-',strtolower($row['name']));?> " class="btn btn-md btn-bitbucket"><i class="fa fa-user"></i> View Profile</a>

I get a page not found error. Ive also tried

  <a href="engineer-profile/<?php echo str_replace(' ','-',strtolower($row['country']));?>/<?php echo str_replace(' ','-',strtolower($row['area'])); ?>/<?php echo $row['id'];?>/<?php echo str_replace(' ','-',strtolower($row['name']));?> " class="btn btn-md btn-bitbucket"><i class="fa fa-user"></i> View Profile</a>

Ive also checked mod rewrite is on and it is as it works with basic testing fine. Can someone please explain where I'm going wrong and how I can fix it?

1
  • Your numbers (23) don't match [a-zA-Z_]+). Commented Jul 31, 2015 at 14:04

3 Answers 3

2
engineer-profile.php ( example )
--------------------------------
<?php
   echo '<pre>';
   print_r( $_GET );
   echo '</pre>';
?>


<?php
    $id=$row['id'];
    $country=str_replace(' ','-',strtolower($row['country']));
    $area=str_replace(' ','-',strtolower($row['area']));
    $name=str_replace(' ','-',strtolower($row['name']))

    echo "<a href='/$country/$area/$id/$name' class='btn btn-md btn-bitbucket'><i class='fa fa-user'></i>View Profile</a>";
?>

# .htaccess
#  required format
#  /uk/southport/23/company-name/

# the following requires that rewritebase is specified, viz:-
RewriteBase /

RewriteRule ^([a-zA-Z\_\-]+)/([a-zA-Z0-9\_\-]+)/([0-9]+)/([a-zA-Z0-9\_\-]+)(/?)$ /engineer-profile.php?country=$1&area=$2&i=$3&company=$4 [NC,L]

# there were a couple of omissions from original - this works now though. The demo engineer-profile.php will output the following

Array
(
    [country] => uk
    [area] => southport
    [i] => 23
    [company] => company-name
)
Sign up to request clarification or add additional context in comments.

1 Comment

awesome. When I use this example and thank you by the way @RamRaider I just get page not found. engineer-profile.php is definitely there though as it was working before mod rewrite
1

Your URL doesn't match the route. You need to expand what you allow into the rule.

RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_0-9]+)/([a-zA-Z_]+)(/?)$ engineer-profile.php?country=$1&area=$2&i=$3&name=$4 [L]

4 Comments

cool am I right with my actual link as I think thats the bit I'm not really understanding. When I link to the page I link with /uk/southport/1/company/ or do I use the original link and mod rewrite rewrites the name for me?
ive tried both and neither work. Just getting the page not found
Nope, it won't convert it for you - you need to use the new link (/uk/southport/1/company) I've just noticed you have an end slash on the end. I'll edit my answer
sorry here is a proper link. if you search for builder, uk, pr97tf you will see where the link is made. its when you click profile. parrotsearch.co.uk/search-form.php
0

As your using strtolower() and str_replace() spaces to dashes in your link generation, and have numbers in there.. ..your rewrite rule cases should be ([0-9a-z\_\-]+) to allow numbers, lowercase letters, underscores and dashes, and no need for upper case values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.