user1612250
user1612250

Reputation: 151

Twitter alike htaccess rewrite

I want to give users profile urls as domain.com/their_username

So I need all alphanumeric character words except few to be rewritten

This is what I have came up with but it is not working

RewriteRule ^(\w+)$ test.php?f=first&s=$1 [NC,L]

RewriteRule ^((admin|def|images|includes|profile|uploads|users)(.*)) test.php?f=second&s=$2$3 [NC,L]

RewriteRule ^p/(.*)$ index.php?v2=$1 [NC,L]

RewriteRule ^search/((.*)/)?(.*)$ index.php?v=search&v2=$2&s=$3 [NC,L]

RewriteRule ^(faq|privacy|terms|contact|verify)$ index.php?v=$1 [NC,L]

Upvotes: 0

Views: 146

Answers (1)

doublesharp
doublesharp

Reputation: 27637

You need to move your first rule to the bottom or it will match any alphanumeric characters with the ^(\w+)$ pattern and then stop processing because of the [L]ast directive. As currently written one of your other rules will ever match because that pattern will match "admin", "search", or "myusername". The "directory" pattern was changed to a character match of anything that is not a forward slash, this usually works better for URL matching than the . wildcard as it can be greedy. I typically like to include a RewriteBase as well.

# Enable mod_rewrite and set the base directory
RewriteEngine on
RewriteBase /

# These are directories (directory/SOMETHING)
RewriteRule ^(admin|def|images|includes|profile|uploads|users)/([^/]*) test.php?f=second&s=$2$3 [NC,L]
# Matches p/SOMETHING
RewriteRule ^p/([^/]*)$ index.php?v2=$1 [NC,L]
# Matches search/SOMETHING/QUERY
RewriteRule ^search/(([^/]*)/)?(.*)$ index.php?v=search&v2=$2&s=$3 [NC,L]
# Matches directory (with or without trailing slash)
RewriteRule ^(faq|privacy|terms|contact|verify)/?$ index.php?v=$1 [NC,L]

# No other rules matched, assume this is a username
RewriteRule ^(\w+)$ test.php?f=first&s=$1 [NC,L]

You can test using http://htaccess.madewithlove.be/

Upvotes: 1

Related Questions