Vanity URLS: mod_rewrite and SQL query return bad flag delimiters error

314 Views Asked by At

I'm using a very simple method to enable vanity URLS on my website and yet, i get a 500 server error.

I want to enable URLs like this : mysite.com/ben instead of mysite.com/profile.php?id=2

Here's the code i'm using in my htaccess file:

RewriteCond %{REQUEST_FILENAME}.php -f  
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile.php?username=$1 [L]</pre>

Here's the query at the top of profile.php:

$getName = explode("/",$_SERVER['REQUEST_URI']);
$result = query("SELECT * FROM users WHERE username='$getName[3]'");  
if (count($result) == 0)
 {
    header ('Location:404.php'); 
 }

Now, with this script I get a 500 server error on any page I try to access. In my Apache error log, I can see: RewriteRule: bad flag delimiters

What does this mean?

3

There are 3 best solutions below

2
On BEST ANSWER

OK, this is going to sound kind of dumb, but looking at the link that you posted here: http://culttt.com/2011/11/16/how-to-make-vanity-urls-using-php-htaccess-and-mysql/

The </pre> at the end of their example will cause a 500 server error. And, not surprisingly, when I look at my error logs, it says:

 /var/www/localhost/htdocs/.htaccess: RewriteRule: bad flag delimiters

So remove the </pre> and change the &gt; to >.

0
On

RewriteCond %{REQUEST_FILENAME}.php -f

There is extra white space at the end of this line after the -f flag.

0
On

Try to replace your .htaccess code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

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