To simply redirect all urls that do not have a /
within them, you could achieve with a simple AliasMatch (from mod_alias):
AliasMatch ^/([^/]+) /path/to/handler.php
You could then look at the REQUEST_URI
environment variable in your script to process
the user to display.
And then for additional stuff like /about
handling:
AliasMatch ^/([^/]+)/(about|videos|blah) /path/to/handler.php
or you could get it to match just two levels (so paths like /x/y/z
will not be handled) in the same way we matched the user part above ([^/]+
one or more characters that are not a slash):
AliasMatch ^/([^/]+)/([^/]+) /path/to/handler.php
And again use the REQUEST_URI
to work out what to display.
This will serve the page URL "as-is" (so the url stays the same on your location bar in your browser) yet the content will be generated by /path/to/handler.php
.
These would be two separate rules. There would probably be a way to handle just a /username
and /username/something
in one rule, but then it would most likely end up being quite unreadable. They are done in this fashion to avoid matching everything after domain.com
.
If you need to change to path into arguments to the script, then you are into the land of RewriteRule
(from mod_rewrite). If you want to maintain the URL in the location bar
you will have to do a reverse proxy query:
RewriteEngine On
RewriteRule ^/([^/]+) /path/to/handler.php?user=$1 [LP]
RewriteRule ^/([^/]+)/(about|videos|blah) /path/to/handler.php?user=$1&args=$2 [LP]
This would save the script having to process REQUEST_URI
, and can pull variables from $_GET['user']
and $GET['args']
if using PHP.
Again, like AliasMatch
that second rule could be:
RewriteRule ^/([^/]+)/([^/]+) /path/to/handler.php?user=$1&args=$2 [LP]
but note for both of these, your script will have to know how to handle everything that matches two urls. This may not be what you want for such urls as:
/errors/404.html
/about/google.html
and so on.