Tutorial :Pretty Profile URLs without conflicts with pages



Question:

I want to use profile URLs on my site such as xyz.com/username

I am using the follow code:

RewriteRule ^([a-zA-Z0-9_-]+)$  index.php?p=profile&u=$1 [L,QSA]    RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=profile&u=$1 [L,QSA]  

My question is... How can I use it like this, and keep the access to other links such as xyz.com/forums, xyz.com/friends, etc..

Thank you.


Solution:1

Use this:

RewriteCond %{REQUEST_FILENAME} !-d  RewriteCond %{REQUEST_FILENAME} !-f  RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p=profile&u=$1 [L,QSA]  

It checks whether the requested file is a directory or file, and if not passes it off to index.php

You can also put a ? after the / to make it optional (combining your two rules).


Solution:2

You can try using a condition:

RewriteCond %{REQUEST_FILENAME} !-f  RewriteCond %{REQUEST_FILENAME} !-d  RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p=profile&u=$1 [L,QSA]  

The -f and -d are flags for "is a file" and "is a directory" respectively. ! negates that. Your rewrite should only happen for urls that don't actually exist in your web root. You'll probably want to add an initial condition to match against your username format so you don't stomp on every potential 404 error.

You could prepend the following, too:

RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9_-]+/?$  

So you'll only match /adsfasdfasdf instead of /something/that/doesn't/exist


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »