Web Hosting Talk







View Full Version : Mod rewrite without using extensions


lexington
06-03-2008, 09:56 AM
Hello, I am trying to make it so that profile.php is rewritten so that a user can call up his profile with the url such as:

blah.com/username

I tried using this mod rewrite:

RewriteRule ^(.*)\/$ profile.php?profile_value=$1 [L]

It works for:

blah.com/test/9/

but if the slash is missing at the end it would display a page not found error. Not to mention it seems to break link link on the page that doesn't contain a path such as /templates/. I currently use templates/ without the first slash and when using the mod rewrite that I shown above it breaks that link. So would I just have to go through and change all of those paths? Anyway, if anything if you could provide me with the coded needed to make both:

blah.com/test/9/

and

blah.com/test/9

lead to the same page that would be great. Thanks!

Dark Light
06-03-2008, 11:14 AM
Have you tried using a regular expression similar to the following:
^(.*)\/[0-9]+(\/)?$
It basically is the same as you used previously but it requires that there be a string, followed by a forward slash, followed by any amount of numeric characters with an optional forward slash at the end. :)

Hope that helps,

lexington
06-03-2008, 11:30 AM
I tried:

RewriteRule ^(.*)\/[0-9]+(\/)?$ profile.php?profile_value=$1 [L]

and it gave a 404 error. Also I am not using the htaccess from the root directory this htaccess file is within a sub directory for now.

Dark Light
06-03-2008, 11:53 AM
Forgot I was working with the second part. :D

Here is what should do it for you:
RewriteEngine on
RewriteRule ^(.*)\/([0-9]+)(\/)?$ profile.php?profile_value=$2 [L]

Steve_Arm
06-03-2008, 12:54 PM
Yes because using only (.*) will catch the whole url. Like the above example
will pass everything to the profile page. I think it is:

RewriteRule ^profile/(.*)(\/)?$ profile.php?profile_value=$2 [L]

or more strict
RewriteRule ^profile/([a-zA-Z]+)(\/)?$ profile.php?profile_value=$2 [L]

which will only accept profile/username and not profile/username/foo,
for which you can add the whatever kind of part after the (\/)

Dark Light
06-03-2008, 01:00 PM
Yes because using only (.*) will catch the whole url. Like the above example
will pass everything to the profile page. I think it is:

RewriteRule ^profile/(.*)(\/)?$ profile.php?profile_value=$2 [L]

or more strict
RewriteRule ^profile/([a-zA-Z]+)(\/)?$ profile.php?profile_value=$2 [L]

which will only accept profile/username and not profile/username/foo,
for which you can add the whatever kind of part after the (\/)
Assuming the first post indicated that the author wanted an identification number (numeric ID) to be passed to profile_value, your example wouldn't work. However, as the first post was unclear, I could understand the confusion.

Yours actually would match www.example.com/profile/username, and not www.example.com/profile/username/1/ as indicated in the first post.

Additionally, you're using $2 when that variable would always equal to /, whereas my example above was using $2 because it would output a numeric value.

Hope that helps,

Steve_Arm
06-03-2008, 01:08 PM
You are right, from an application point of view I assumed that a username would
go to a profile page ;), but yes he wants to pass it after the domain.

About the back reference it was 2 because I copy/pasted it from your line.

lexington
06-03-2008, 01:36 PM
Yours actually would match www.example.com/profile/username, and not www.example.com/profile/username/1/ as indicated in the first post.

Yes actually the script does both. I have an if statement that checks if the value is_numeric and if so it passes the number, and if not it passes the other characters. That way a user can access their profile via ID or name.

lexington
06-03-2008, 01:38 PM
Sorry but:

RewriteRule ^(.*)\/([0-9]+)(\/)?$ profile.php?profile_value=$2 [L]

Gave me a 404 error.

lexington
06-03-2008, 02:05 PM
If the only reason why this doesn't work is due to numeric or other characters, I suppose I can just make it display characters only then.

Dark Light
06-03-2008, 02:22 PM
If it's giving you a 404 error, either your RewriteEngine isn't turned on, or possibly not finding your profile.php file. Possibly your URL isn't conforming to the regular expression, which would be where the .htaccess is placed in relation to the web root.

I tested the above rule with my Apache 2 + PHP setup and it worked flawlessly for me, even when the file is situated below the web root (public_html).

Can you expand on the 404 error by pasting the actual error?

lexington
06-03-2008, 03:22 PM
So it has to be in the root? I will try that then thanks. Well, where do I add the directory name in that code then? Since the files are currently being tested in a sub directory on my main site.

lexington
06-03-2008, 03:25 PM
Oh wait I forgot to comment out the htaccess file in my test directory but it still doesn't work unless I added the directory in your new code incorrectly.

Dark Light
06-03-2008, 03:29 PM
I think you've miss-understood what that regular expression is designed to do.

RewriteRule ^(.*)\/([0-9]+)(\/)?$ profile.php?profile_value=$2 [L]
That catches test/9/ or test/9 or any a-z characters before the / and then 0-9 characters afterwards.

Therefore, if you explicitly want to use test/ then use:
RewriteRule ^test\/([0-9]+)(\/)?$ profile.php?profile_value=$1 [L]
The above would allow blah.com/test/9 with or without the slash at the end. However, I think you also wanted to accept /test/username?

I assumed the /test/ would be the username, but if the bit after the /test/ can be either a numeric value OR a string, then you might look into something like this:
RewriteRule ^test\/([a-zA-Z0-9]+)(\/)?$ profile.php?profile_value=$1 [L]

Because only you know what you are trying to do, you may have to play around with it a bit. :)

Hope that helps,

lexington
06-03-2008, 03:34 PM
Darn nothing is working and I am stressed out so I will have to lay down but thanks. The weird thing is that:

RewriteRule ^(.*)\/$ profile.php?profile_value=$1 [L]

works perfectly except if there is no slash on the end it will not work. Not sure why it has to be rewritten so drastically just to make it work with both the slash and without the slash on the end.

Tim Greer
06-03-2008, 08:04 PM
If that's working and doing what you want it to do, then does simply adding the optional "?" work for you (without the parenthesis)?

RewriteRule ^(.+)\/?$ profile.php?profile_value=$1 [L]

lexington
06-04-2008, 06:39 AM
Weird that doesn't work either. Ok perhaps I need to go about this a different way. How come when you visit soundclick, you can enter the artist name such as: (just grabbing any random page here):

http://www.soundclick.com/glennbo

and it redirects to the user's page?

Dark Light
06-04-2008, 06:48 AM
Most likely, SoundClick would use some rewriting rule and/or a controller to check if "glennbo" exists as a real page of content, and if it doesn't exist as one, does it exist as a username? If so, display that username profile. If none of those, most likely return a 404 error.

I think you're adding the RewriteRules in the wrong place, using them in the wrong situation or describing what you want to do incorrectly; we've provided a number of RewriteRules that would work in a variety of different situations; most of which have been tested on our own servers.

Best Regards,

lexington
06-04-2008, 06:57 AM
I use rewrite as well and never had any problems until now. I just basically want it to work and it does work perfectly the only thing is that if there is no slash at the end it breaks and I would like it to work with a slash and without.

Dark Light
06-04-2008, 07:05 AM
If that's working and doing what you want it to do, then does simply adding the optional "?" work for you (without the parenthesis)?

RewriteRule ^(.+)\/?$ profile.php?profile_value=$1 [L]

Tim Greer gave the answer away of how to make the forward slash optional. The ? indicates that the previous item is optional, so if the above doesn't work for you, try adding () around the \/.

e.g.

^(.+)(\/)?$

lexington
06-04-2008, 07:06 AM
I have sent you a pm with how my page works thanks :)

lexington
06-04-2008, 07:38 AM
Dark Light solved the problem when he viewd my test site thanks again :D

lexington
06-04-2008, 09:38 AM
Oops I meant to create a new topic