Extreme noob question (hopefully)

New to mod_rewrite? This is a good place to start.

Extreme noob question (hopefully)

Postby stuffonmymate » Mon Jul 27, 2009 3:28 am

Hi all,
It's my first post so please go easy on me. I have had a search around the forums but have not been able to find what I am looking for. I am having my first stab at mod rewriting and need some help to reditect as follows

site.com/myuser or site.com/myuser/
to
site.com/?user=myuser

BUT I do not want to redirect if the following URLs are entered

site.com/<directory_name> or site.com/<directory_name>/
e.g.
site.com/upload or site.com/upload/
site.com/story or site.com/story/
site.com/account or site.com/account/

As these are actual directories on my web server. I hope this is possible and would really really appreciate some help.

Sanj.
stuffonmymate
 
Posts: 2
Joined: Mon Jul 27, 2009 3:12 am

Postby richardk » Mon Jul 27, 2009 8:26 am

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?user=$1 [QSA,L]

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore specific directories.
RewriteCond %{REQUEST_URI} !^/(account|story|upload)(/.*)?$
RewriteRule ^([^/]+)/?$ /?user=$1 [QSA,L]


You can also ignore specific files.
Code: Select all
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|robots\.txt)$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby stuffonmymate » Tue Jul 28, 2009 12:51 am

wow, that's awesome, worked like a charm.

thanks a lot for your help.
stuffonmymate
 
Posts: 2
Joined: Mon Jul 27, 2009 3:12 am

Postby alan543 » Sat Aug 08, 2009 6:50 am

Thanks, this is exactly what I needed too. I now have mysite.com/user going to mysite.com/index.php?user=$user.

I was wondering if a rule needs to be made for each page? for example after going to mysite.com/user and then they go to mysite.com/user/otherpage.php it will need to be rewritten as mysite.com/otherpage.php?user=$user

Is there anyway to achieve this with one rule rather than one for each page? I don't want to pass the user variable in the URL.

Thanks in advance!
alan543
 
Posts: 1
Joined: Sat Aug 08, 2009 6:41 am

Postby richardk » Wed Aug 12, 2009 11:27 am

Something like
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

# Check the PHP file exists.
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+\.php)$ /$2?user=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 29 guests

cron