Mod_Rewrite Help

I have a .htaccess file located at “http://localhost/social/” that contains the following code.

RewriteEngine  on
RewriteRule ^/social/([^/]+)$ /user.php?user=$1

When I type “http://localhost/social/USERNAME” I get a “Not Found” error message saying “The requested URL /social/USERNAME was not found on this server.” instead of sending me to “http://localhost/social/user.php?user=USERNAME” I dont’ know what the error is and I am new to mod_rewrite. Could somebody help me, or tell me my error?

I am assuming the document root is accessed at http://localhost since you’re including /social in the rule.

In that case, /user.php would reference http://localhost/user.php not http://localhost/social/user.php

Starting any path with a forward slash means it’s relative to the root of the site, not to the current directory

I still get that same error. Here is my updated code.

<IfModule mod_rewrite.c>
RewriteEngine  on
RewriteRule ^/social/([^/]+)$ /social/user.php?user=$1
</IfModule>

:tup: Dan!

Moreover, BMorganVA, if your .htaccess file is located in the social directory, it will NEVER see ^/social/{yadda-yadda}! Basically, it cannot see the directory it’s in.

Next, if you move this mod_rewrite code to the DocumentRoot (as Dan inferred), then Apache 2.x would choke on the leading / (Apache 1.x would REQUIRE it). Since you didn’t mention which flavor you’re using, then use ^/? which satisfied both cases.

Regards,

DK

I’m using Apache 2.2.14.

Here is my current code…and it still doesn’t work :mad:.

<IfModule mod_rewrite.c>
RewriteEngine  on
RewriteRule ^/social/([^/]+)$ ^/social/user.php?user=$1
</IfModule>

BTW, this is my first time using mod_rewrite.

Should I take the <IfModule) tags away?

Please help. Any help I could get with this would help a ton! Thanks!

:slight_smile:

Try what DK recommended in post #4

Yes, but what I recommended was to use THIS in the DocumentRoot (NOT the social subdirectory):

RewriteEngine  on
RewriteRule ^social/([^/]+)$ ^/social/user.php?user=$1 [L]

If you HAVE to put it in social:

RewriteEngine  on
RewriteRule ^([^/]+)$ ^/social/user.php?user=$1 [L]

Regards,

DK

BTW, incorporating the <IfModule> braces means that the test is made for EVERY request - a TOTAL WASTE of server resources and a KILLER on a shared host! It’s there because the WP people don’t want their code to give the clueless masses an Error 500 because they don’t have mod_rewrite enabled.

Regards,

DK

Here is my code.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^social/([^/]+)$ social/user.php?user=$1 [L]

The error message (in Firefox) says, “Firefox has detected that the server is redirecting the request for this address in a way that will never complete.” 500 error shows up when the second part of the rule starts with / and the URL can’t be found if it starts with “^” or “^/”.

Can anybody help please? Thanks so much. If I didn’t already say this, this will help my site a lot!

BM,

Since you’re new to regex, you may not recognize that ([^/]+) is the same thing as (.*) but excluding the / character. Saying that, you should recognize that user.php IS MATCHED, i.e., YOU are driving the server LOOPY. You can avoid this by using a RewriteCond which either looks for the user key in the {QUERY_STRING} OR merely excluding user.php.

Moreover, RewriteBase is to be used to UNDO mod_alias redirects so that, if this is your entire .htaccess file, it has NO BUSINESS in your directives.

Regards,

DK

I don’t even know what (.*) means.

I’m trying to find out how do get the condition that you suggested. Could you, or somebody else please show and example? I’m trying to learn from this page, but it’s not that helpful. Thanks!

One more thing, does the RewriteCond go before or after the rule?

:slight_smile:

BM,

Did you look at the TUTORIAL linked in my signature for code?

WARNING! I treat those asking for me to code for free as “script kiddies.” I’m here to help you LEARN but get upset at those asking me to code for them. Just minimal effort on member’s part will gain a LOT of help from me!

Regards,

DK

I totally understand what you said. Please forgive me. :blush:

One question, in your tutorial do I replace %{QUERY_STRING} with something, or leave it like that?

:Partier:

Thanks for the update!

BTW, with that quick (albeit long with everything that’s there) tutorial, you should be an expert now! Horray!

Regards,

DK

I am confused out of my mind! This is so challenging. Here is my current code.


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} !user\\.php=
RewriteRule ^/?social/([^/]+)$ social/user.php?user=%{QUERY_STRING} [NC,L]

I’m getting that same error message that I was before, and the message shows up on all pages.

I do have a few questions. First, in {QUERY_STRING} do I enter my own value or not? Second, I can’t figure out how to grab the user name out of the {QUERY_STRING} (if it is even contained in that variable). Finally, I don’t know how to get rid of the “user.php” part and I don’t know what DK means by “excluding it”. I’m not asking for an entire scrpit, I’m just asking for general answers. This is my first time using mod_rewrite.

Thanks for you help!

AH-HA! This is starting to make some sense. :smiley:

Here is my current code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} user
RewriteRule ^/?social/([^/]+)$ social/user.php?user=%{QUERY_STRING} [NC,L]

When I type “http://localhsot/social/USERNAME” it says that URL doesn’t exists.

Is there anymore suggestions people may have? Thanks for the help! :slight_smile:

PS If you ahven’t notieced what I’m doing, I making a Profile URL (ie http://twitter.com/USERNAME).

Your RewriteCond (a CONDition that must be true for the following rules to apply) says that the query string must contain the word “user”

http://localhost/social/USERNAME

is a URL with no query string at all, so it doesn’t match and the rule isn’t applied

If that’s what your URLs will look like then you do not have a query string and don’t need a RewriteCond to match any query string. You’re creating a query string where there was none, and it’s based on the username in the URL.

Something like

RewriteRule ^/?social/([^/]+)$ social/user.php?user=$1 [NC,L]

…where $1 refers to the first group of matched text, identified by the parentheses () around the portion fo the URL that is matching the username

OK. I made that change, but now, on every age I got to, all it displays is “user.php”.

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?social/([^/]+)$ social/user.php?user=$1 [NC,L]

Isn’t it supposed to?

What URL are you accessing that you don’t want to map to user.php?