Mod_rewrite: domain . com / username ....help needed!

Hi there,

For a website i’m building i’d like to make it easier for users to access their profiles by typing in domain.com/username instead of domain.com/user/username.

I’ve looked at a few mod_rewrite tutorials and have a vague notion that I need to check whether the path is a file or directory or not, if it isn’t then rewrite the path to /user/?user=username. I’ve tried applying this into my .htaccess but it comes up with an error whenever I try to load a page.

I’ve included my .htaccess file below in the hope someone can help, thanks :slight_smile:

# set error documents
ErrorDocument 500 /error.php
ErrorDocument 501 /error.php
ErrorDocument 502 /error.php
ErrorDocument 503 /error.php

# disable etag configuration
Header unset ETag
FileETag None

# set default local timezone
SetEnv TZ Europe/London

Options +FollowSymLinks

RewriteEngine On

# remove www in addresses
RewriteCond %{HTTP_HOST} ^**********
RewriteRule (.*) **********/$1 [R=301,L]

# force trailing slashes for non-files
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [R=301,L]

# remove extension for direct .php requests
RewriteCond %{THE_REQUEST} ^[A-Z]+\\ /([^/]+/)*[^.#?\\ ]+\\.php([#?][^\\ ]*)?\\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\\.php /$1 [R=301,L]

# specific rules
RewriteRule ^post/(.*)/$						/post/?slug=$1						[QSA,L]
RewriteRule ^feeds/user/(.*)/$					/feeds/user/?user=$1				[QSA,L]

# redirect username (/username/ > /user/?user=username)
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)/$ /user/?user=$1 [QSA,L]

# /file/ to /file.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)/$ /$1.php [QSA,L]

MS2,

First, Welcome to SitePoint’s Apache forum.

It appears that, while you have SOME understanding of mod_rewrite, you’ve fallen into TWO ‘noobie traps,’ i.e., looping on using mod_rewrite to change the format of your URIs (then changing back to fetch the file) AND using lazy regex (without understanding the problems you will cause with the resulting loops).

In my signature’s tutorial, I discuss how the extensionless URIs can be done (I’d originally answered a question years ago that it was not possible to remove the extension then fetch the file with the extension - but had another think about it). Since you did not take the care to add a “marker” to your loopy redirection (to prevent the loop), Apache MUST (get dizzy then) FAIL the request.

RewriteEngine On

# remove www in addresses
[COLOR="Blue"]# replace your domain name with example.com
# - it makes checking your syntax easier[/COLOR]
RewriteCond %{HTTP_HOST} ^**********
# RewriteRule (.*) **********/$1 [R=301,L]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
[COLOR="Blue"]# WHY bother capturing the {REQUEST_URI} when it's already available?[/COLOR]

# force trailing slashes for non-files
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [R=301,L]
[COLOR="Red"]# IMHO, this is HORRIBLE as it changes directory level[/COLOR]

# remove extension for direct .php requests
RewriteCond %{THE_REQUEST} ^[A-Z]+\\ /([^/]+/)*[^.#?\\ ]+\\.php([#?][^\\ ]*)?\\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\\.php /$1 [R=301,L]
[COLOR="Red"]# As above AND {THE_REQUEST} is a terrible variable to deal with[/COLOR]

# specific rules
RewriteRule ^post/(.*)/$ /post/?slug=$1 [QSA,L]
RewriteRule ^feeds/user/(.*)/$ /feeds/user/?user=$1 [QSA,L]
[COLOR="Blue"]# "Lazy regex" will almost invariably lead to problems
# Specify the acceptable characters instead[/COLOR]

# redirect username (/username/ > /user/?user=username)
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)/$ /user/?user=$1 [QSA,L]
[COLOR="Blue"]# I can't say I'm fond of abusing the server by making it
# go fetch the DirectoryIndex when you'll not be showing it anyway[/COLOR]

# /file/ to /file.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)/$ /$1.php [QSA,L]
[COLOR="Red"]# One or more of any character followed by /?  Argh![/COLOR]

As for your lazy regex, I can only say that those who use (.*) get what they deserve (loops on the server - thank goodness Apache 2 learned to terminate these induced loops rather than allow the whole server to be taken down!).

Regards,

DK

Hi dklynn,

Thanks for welcoming me to the forum.

Clearly you have some sort of mental instability that is blurring your judgement on how to help people …especially new members… on this forum.

I spent the time registering and posting on this forum because of the reputation of SitePoint (I’ve been buying SitePoint books for years) and to get some much need help and advice (I’ve only just started learning mod_rewrite); not for some idiot to rip apart my code and say that “get what I deserve”

When you first started using mod_rewrite, all those years ago, did you know everything you know today? Did you ask for advice on forums such as these? If you’re over 30 then I genuinely feel very, very sorry for you.

I really doubt I trust the SitePoint brand as much I used to after this.

[FONT=“Georgia”]whistle

MagSafe2. Chill dude.

Instead of getting offended, why not learn from what dklynn is trying to demonstrate?

Consider that he took quite a bit of his time, out of his own free will, to read through your problem, analyse it, and reply with solutions. He could more easily have left you up your creek.

[/FONT]

Cool it, please.

Thanks, Shaun and raena.

MS2,

Perhaps you read my comments as a personal attack. PLEASE BE ASSURED that was NOT my intent. If I had intended to insult you (or anyone else), I’d be the first to admit it and chastise myself!

Yes, I get frustrated at seeing and commenting on the same questions day after day, week after week, …, you get the idea. A little bit of search through this forum would have presented you with the same answers I gave above as would have been a read through the tutorial (which is also the FIRST sticky in the forum’s page).

If you don’t like the (technical) answer to your question, there was still no reason to respond the way you did. I did not infract you for your response as the misinterpretation of my comments was obvious.

As I’ve repeated in other recent posts, I’m NOT here to do free coding. I believe that my value to the forum is in teaching members. You correctly presented your code and I attempted to point out the errors there. I’m more than willing to continue to help you.

Regards,

DK

MagSafe2, I hope DK’s post reassures you that he didn’t intend to attack you personally.

Let’s all of us try and be a bit kinder when we answer questions like these.

Hi MagSafe2, welcome to the forums.

Knowing mod rewrite requires knowing 2 things (at least), Apache and regex.
From what I’ve seen in the forum, most problems are from a lack of understanding about how regex works. Many are satisfied if they can capture what they want. But it is also important to Not capture what you don’t want so you capture Only what you want.

The everything atom (I shudder everytime I see it used) is perhaps the most common offender. Sure, the result set “everything” will capture what you want, and maybe sometimes it will have no visible adverse effect if trash is included, eg. the PHP code “filters out” and “cleans up” any mess. But AFAIK, Apache has no way of determining what you intended, only what you specified, right or wrong.

I commend you for not blaming “stupid Apache”, as some have done, realizing the fault is with your code, and seeking advice.

And it looks like you have some of the basic mod rewrite stuff down. Many just copy/paste flags from other’s code without even knowing what they do.

Be patient, IMHO regex has a steep learning curve, but it can be done. The aforementioned sticky will help a lot.

I’ve ordered a book on the subject (not something SitePoint has published) and will learn what I need from there.

Could an administrator please delete my account?

Thanks.

@MagSafe

We don’t delete accounts as spammers could use the same names in the future or the names of old real members that wanted to leave SP. In this way they can’t use it. I think it also has to do with the integrity of the information in the database (posts would be orphan and so).

While you may not like the tone (I confess that I can’t understand why), the answer is still valid and hopefully it can help someone else.

I am under the impression that you have believed that expressions like “lazy regex” are an insult… If that’s the case, you should google it as it is a technical term.