Chidi Nkwocha
Chidi Nkwocha

Reputation: 1

vanity url with .htaccess in php

I'm trying to create a vanity url for where users profile could easily be viewed by others.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule .* - [L]

RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]

This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. What am i getting wrong? Do i need any to do any configuration first?

Upvotes: 0

Views: 655

Answers (2)

sariDon
sariDon

Reputation: 7971

Check this:

    Options +FollowSymLinks
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
    </IfModule>

However note:

  1. You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working)
  2. Put proper RewriteBase directive if you use it from subdirectory.

Upvotes: 0

num8er
num8er

Reputation: 19372

It's better to use global rewrite logic:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure.

But if You insist on realization as in question so try this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]

Upvotes: 1

Related Questions