sameer kumar sameer kumar - 3 months ago 8
PHP Question

.htaccess rewrite rule for username and index along with other pages

I am developing one website of resume upload, user can register and submit there resume, later they ll see there resume by entering a custom link like yourdomain.com/profile/username,
now its all done but the thing is i rewrite an htaccess

RewriteRule ^profile/([^/]+)/?$ profile_view.php?user_custom=$1 [L,QSA,NC]


now I have the problem is the client wants the URL like yourdomain.com/username

but when I rewrite the
.htaccess
to

RewriteRule ^([^/]+)/?$ profile_view.php?user_custom=$1 [L,QSA,NC]


by removing the
"profile/ "
then when ever am passing the username like yourdomain.com/username

there is no problem but am facing the problem when i open
yourdomain.com/index.php
the index page lost along with all other pages.

Can any one suggest a solution?

Answer

You should exclude file and directories from your rule by adding RewriteCond just before your rule:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]+)/?$ profile_view.php?user_custom=$1 [L,QSA,NC]