DANGER! simple mod_rewrite question... o_O

I’d like: http://www.example.com/username

to redirect to: http://www.example.com/user.php?id=username

Trying to use this mod_rewrite rule I found: RewriteRule ^user/(\w+)/?$ user.php?id=$1

However, I have something wrong. It doesn’t work (shocker!). I’ve tried a lot of different things, but no luck. Any advice linode ninjas? Thank you!

ServerAdmin webmaster@localhost

ServerName example.com

ServerAlias http://www.example.com

DocumentRoot /home/public

Options -Indexes FollowSymLinks MultiViews

AllowOverride None

Order allow,deny

allow from all

RewriteEngine On

RewriteRule ^user/(\w+)/?$ user.php?id=$1

ErrorLog /var/log/apache2/error.log

Possible values include: debug, info, notice, warn, error, crit,

alert, emerg.

LogLevel warn

CustomLog /var/log/apache2/access.log combined

RewriteEngine On

RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)

RewriteRule .* - [F]

5 Replies

You could instead use mod_proxy and in the VirtualHost section add these two lines:

ProxyPass /

ProxyPassREverse /user.php

Then update user.php to look at the url and parse the username.

For any directories that should not be part of username processing like:

http://www.example.com/foo

You could tell Apache to pass those urls through as is with:

ProxyPass /foo !

I'll have a look at this mod_proxy and give it a try - thanks!

@eekeek:

Trying to use this mod_rewrite rule I found: RewriteRule ^user/(\w+)/?$ user.php?id=$1
Your RewriteRule here expects a url that looks like: http://www.example.com/user/username, which will redirect to http://www.example.com/user.php?id=username.

If you really want to use http://www.example.com/username, then you are going to have some added complication. What if i register my username as index.php?

@bacon:

@eekeek:

Trying to use this mod_rewrite rule I found: RewriteRule ^user/(\w+)/?$ user.php?id=$1
Your RewriteRule here expects a url that looks like: http://www.example.com/user/username, which will redirect to http://www.example.com/user.php?id=username.

If you really want to use http://www.example.com/username, then you are going to have some added complication. What if i register my username as index.php?

You're right about the incorrect rule, and I don't use Apache but I remember you could have rewrite conditions to make sure a file does NOT exist before rewriting. In that case it would just serve index.php instead of redirecting, so the user loses that functionality. To prevent this, you could just disallow dots in usernames.

Also I'd recommend a rewrite for this purpose instead of a reverse proxy.

@jerzzp:

You're right about the incorrect rule, and I don't use Apache but I remember you could have rewrite conditions to make sure a file does NOT exist before rewriting. In that case it would just serve index.php instead of redirecting, so the user loses that functionality. To prevent this, you could just disallow dots in usernames.

Don't rewrite if it matches a directory:

RewriteCond %{SCRIPT_FILENAME} -d

Or file:

RewriteCond %{SCRIPT_FILENAME} -f

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct