+2 Gary Miller · November 18, 2014
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /lr/profile.php?username=$1

This is my .htaccess and it works very well. If i click a <a> tag that is echoing a username it takes me to that users profile. I want this to work the same for school_name

RewriteRule ^(.*)$ /lr/schoolRegister.php?school_name=$1

I thought using the same line and changing username to school_name would work but it doesn't/ They both won't work at the same time. I have no knowledge in rewrites and would appreciate some help.

Post a Reply


Oldest  Newest  Rating
0 Ron Butcher · November 18, 2014
Can you post what your links look like?

^(.*)$ is a default that takes every input and transfers it to the username variable.  The resolution is going to be either to break the rewrite rules down to be more specific, or to change how your links are handled by your PHP.

One way to do it would be to make you links look like this:
<a href="/profile/username/Ron/">Username Link</a>
<a href="/profile/school/school_name/">School Link</a>

In this case, your .htaccess will look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/username/[_0-9a-z]+/$ /lr/profile.php?username=$1
RewriteRule ^profile/school/[_0-9a-z]+/$ /lr/profile.php?school_name=$1
0 Gary Miller · November 20, 2014

<a href="<?php echo $user_data['username'];?>">Profile</a>

<a href="<?php echo $school_data['school_name']?>"></a>

I'm gonna try to use what you previously put but these are the links. What do you think is the best way to do this?
0 Gary Miller · November 20, 2014
its rewriting but not taking me to the page
0 Ron Butcher · November 20, 2014
Your links are going to need to have a little more data.  And they will need to match the first half of the rewrite rule in the .htaccess file.

Two questions before I give you some more code.
1.  What directory is your .htaccess file in?  Is it in the root directory of your website, or the 'lr' directory?
2.  what directory is the page that has the links in, and what is it's filename. i.e. are the links in /lr/profile.php or a different file?
0 Gary Miller · November 20, 2014
lr is in my htdocs and .htaccess is inside of that...  lr has all my web files in it.

the profile is /lr/profile.php
and the school is lr/schoolRegister.php

.htaccess, profile.php, schoolRegister.php are all in lr 

Also thank you for helping me out
0 Ron Butcher · November 20, 2014
Try this.  Use the .htaccess I listed above, but make your links look like this:
<a href="profile/username/<?php echo $user_data['username']; ?>">Profile</a>
<a href="profile/school/<?php echo $user_data['school_name']; ?>">School</a>

You can test to make sure that the proper variable is being set inserting the line

at the top of the page.  It will print out any variables that have been assigned by the rewrite so you can make sure the rewrite is working.
0 Gary Miller · November 21, 2014
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15

this is what i get when i click on the links. I double checked and copied and pasted your code so idk what could be wrong...

The main reason im doing this is so when you click on a school name or a profile name it takes you to that page. I don't really care what it looks like.
0 Gary Miller · November 21, 2014
Also the username rewrite looks right but the school rewrite looks like this:

But they both have that same error
0 Ron Butcher · November 21, 2014
I just tested this and I have an error in my .htaccess.  I apologize about that.  Here is what it should look like:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile/username/([_0-9a-z]+)/$ profile.php?username=$1
RewriteRule ^profile/school/([_0-9a-z]+)/$   profile.php?school_name=$1

If you are not concerned about how your links look, you could just ditch the .htaccess and have your links go directly to the page you want.
<a href="profile.php?username=<?php echo $user_data['username']; ?>">Profile</a>
<a href="profile.php?school_name=<?php echo $user_data['school_name']">School</a>

For the school rewrite, once you remove all the extra crap that the URL encoders put into the URL you get this error:
Notice: Undefined index: school_name in:/xampp/htdocs/lr/includes/menu.php on line 13

This means that your $user_data['school_name'] variable does not exist.  You need to take a look at your code to see why that variable is not being created.
0 Gary Miller · November 21, 2014
Thanks for all your help but i cant seem to get it to work. Im just gonna ditch .htaccess and do what you said. That works great and im completely happy with it. Thanks!
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.